本文主要和大家分享js获取短信验证码按钮倒计时代码,希望能帮助到大家。
 (function(){
 // 快速咨询
 $('#getCode').click(function() {
 var mobile = $('#mobile').val();//手机号码的值
 var _this = $(this);
 var mobileReg = /^1[3|4|5|7|8]\d{9}$/; //手机号正则
 var phoneflag = mobileReg.test(mobile);
 if (!phoneflag) {
 alert('手机号码格式不正确!');
 } else {
 //User/getCode.html这个是后台接口
 $.post("/User/getCode.html", {
 mobile: mobile
 }, function(data) {
 _this.attr("disabled", "disabled");
 _this.css({background:"#eee"});//设置不可点颜色
 var val = parseInt(_this.val());
 var time = 60;
 _this.val(time + " S");
 time--;
 var timer = setInterval(function() {
 val = time + " S";
 _this.val(val);
 if (time > 0) {
 time--;
 } else {
 //原始颜色
 _this.val("获取验证码").removeAttr("disabled").css({background:"#66CCFF"});;
 clearInterval(timer);
 }
 }, 1000);
 });
 }
 });
 })();效果图: