HTML代码:
<button type="button" class="layui-btn layui-btn-primary layui-btn-fluid" onclick="getNoteCode(this)">获取验证码</button>
JS代码:
//获取短信验证码
var countDown = 60;
function getNoteCode(val) {
var phone = $("input[name='cellphone']").val();
if (phone == "" || phone == null) {
layer.msg("请输入正确的手机号")
} else {
$.ajax({
url: "/Total/LittleBackground/ChangeCode",
data: { phone: phone },
success: function (res) {
if (res.Code == 101) {
var newCode = setInterval(function () {
$(val).attr("class", "layui-btn layui-btn-fluid layui-btn-disabled");
$(val).attr("disabled", "disabled");
//val.setAttribute("disabled", true);
$(val).text( "重新发送(" + countDown + ")" );
countDown--;
if (countDown == 0) {
window.clearInterval(newCode);
val.removeAttribute("disabled");
$(val).attr("class", "layui-btn layui-btn-fluid");
$(val).text( "获取验证码");
countDown = 60;
}
}, 1000);
} else {
layer.msg(res.Msg);
}
}
});
}
}