()這裡用的是input做的點擊發送驗證碼<input type="number" class="input" name="mobile" placeholder="手機號" style="border: none"<input class="hui_kuang"style="width: 30%; ...
()這裡用的是input做的點擊發送驗證碼
<input type="number" class="input" name="mobile" placeholder="手機號" style="border: none"
<input class="hui_kuang"style="width: 30%;text-align: center;height: 42px"onclick="setTime(this)" value='獲取驗證碼'>
<script>
//頁面初始化獲取倒計時數字(避免在倒計時時用戶刷新瀏覽器導致倒計時歸零)
var $getCodeInput = $(".hui_kuang");
var sessionCountdown = sessionStorage.getItem("countdown");
if (!sessionCountdown) {
$(".hui_kuang").val("獲取驗證碼")
} else {
$(".hui_kuang").val("重新發送(" + sessionCountdown + ")");
setCode($getCodeInput, sessionCountdown);
}
//獲取驗證碼
function setTime() {
var remobile = $("#registForm input[name='mobile']").val();
if (!remobile) {
alert("請輸入手機號碼")
return;
}
if (!(/^1[3|4|5|8][0-9]\d{4,8}$/.test(remobile))) {
alert("請輸入有效的手機號碼")
return;
} else {
setCode($getCodeInput, 60);
}
}
//發送驗證碼倒計時
function setCode($getCodeInput, countdown) {
if (countdown == 0) {
$getCodeInput.attr('disabled', false);
// $getCodeInput.removeAttribute("disabled");
$getCodeInput.val("獲取驗證碼");
sessionStorage.removeItem("countdown");
return;
} else {
$getCodeInput.attr('disabled', true);
$getCodeInput.val("重新發送(" + countdown + ")");
countdown--;
}
sessionStorage.setItem("countdown", countdown);
window.setTimeout(function () {
setCode($getCodeInput, countdown);
}, 1000);
}
</script>