本文主要為大家分享一篇手機註冊發送驗證碼倒計時的簡單一實例。具有很好的參考價值,一起跟隨小編過來看看吧,希望對大家有所協助,希望能協助到大家。
如下所示:
()這裡用的是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>