方法①:
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<title>60s</title>
</head>
<script type="text/javascript">
var countdown=60;
function settime(obj) {
if (countdown == 0) {
obj.removeAttribute("disabled");
obj.value="获取验证码";
countdown = 60;
return;
} else {
obj.setAttribute("disabled", true);
obj.value="重新发送(" + countdown + ")";
countdown--;
}
setTimeout(function() {
settime(obj) }
,1000)
}
</script>
<body>
<input type="button" id="btn" value="获取验证码" onclick="settime(this)" />
</body>
</html>
方法②:JQuery实现
注:别忘记在js包中将jquery.min.js放进去
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<script src="js/jquery.min.js"></script>
<script type="text/javascript">
function invokeSettime(obj){
var countdown=60;
settime(obj);
function settime(obj) {
if (countdown == 0) {
$(obj).attr("disabled",false);
$(obj).text("获取验证码");
countdown = 60;
return;
} else {
$(obj).attr("disabled",true);
$(obj).text("(" + countdown + ")s 重新发送");
countdown--;
}
setTimeout(function() {
settime(obj) }
,1000)
}
}
</script>
</head>
<body>
<button id="btn" type="button" onclick="invokeSettime(this)">获取验证码</button>
</body>
</html>