页面代码:
<input type="text" placeholder="请输入验证码" id="code">
<input type="button" value="获取验证码" id="sendMess">
js代码:
var InterValObj; //timer变量,控制时间
var count = 60; //间隔函数,1秒执行
var curCount;//当前剩余秒数
$(function() {
//发送验证码
$("#sendMess").bind("click",function(){
curCount = count;
//设置button效果,开始计时
$("#sendMess").attr("disabled", "true");
$("#sendMess").val("请在" + curCount + "秒内输入验证码");
InterValObj = window.setInterval(SetRemainTime, 1000); //启动计时器,1秒执行一次
$.ajax({
url:contextPath+"/getCheckCode.htm",
async:false,
type:"POST",
data:{"phone":phone},
success:function(msg){
}
});
})
});
//timer处理函数
function SetRemainTime() {
if (curCount == 0) {
window.clearInterval(InterValObj);//停止计时器
$("#sendMess").removeAttr("disabled");//启用按钮
$("#sendMess").val("重新发送验证码");
}
else {
curCount--;
$("#sendMess").val("请在" + curCount + "秒内输入验证码");
}
}
这里的刷新验证码为 input 标签,注意往 input 标签赋值和 div 赋值的区别