话不多说直接上代码
function hqCode(){
var tel = $("#tel").val();
if(tel==""){
alert("请输入手机号");
return false;
}
var rule=/^[1][3,4,5,7,8][0-9]{9}$/; //正则表达式验证手机号
if(!rule.test(tel)){
alert("请输入正确格式的手机号");
return false;
}
$.ajax({
type: "POST",
url: '<%=basePath%>login/sendCode.do', //请求后台发送验证码
data: {tel:tel},
success: function(map){
if(map.sendNO==1){ //返回1表示验证码发送成功
//获取短信验证码
$("#hqCode").attr("onclick",""); //解绑获取验证码按钮点击事件,防止多次点击
$("#hqCode").html("验证码发送成功");
$.code =map.code; //把验证码存储到全局变量
var status=true;
var time=60; //倒计时事件设置为60s
if (status) {
status=false;
var s=setInterval(function () { //s表示当前事件id
time--;
$("#hqCode").html(time+"秒后重新获取");
if (time==0) {
clearInterval(s); //取消setInterval()的timeout。即停止循环执行
$("#hqCode").html("重新获取");
$("#hqCode").attr("onclick","hqCode()"); //再次绑定点击事件
status=true;
}
},1000)
}
}else{
$("#hqCode").html("获取失败!");
}
}
});
}