JavaScript实现点击按钮后变灰避免多次重复提交
更新时间:2013年07月15日 17:55:18 作者:
注册的时候需要发送验证激活帐号的邮件,为了避免邮件的多次重复发送,所以可以在点击了发送后,设置按钮变灰,倒计时一段时间后又可重复点击,具体实现如下,感兴趣的朋友可以参考下哈
注册的时候需要发送验证激活帐号的邮件,为了避免邮件的多次重复发送,所以可以在点击了发送后,设置button要过一段时间才能继续点击。下面是一个简单的例子:
<html>
<head>
<title>点击获取验证码按钮后按钮变灰,倒计时一段时间后又可重复点击</title>
</head>
<body>
<input type="button" id="btn" value="免费获取验证码" />
<script type="text/javascript">
var wait=60;
function time(o) {
if (wait == 0) {
o.removeAttribute("disabled");
o.value="免费获取验证码";
wait = 60;
} else {
o.setAttribute("disabled", true);
o.value=wait+"秒后可以重新发送";
wait--;
setTimeout(function() {
time(o)
},
1000)
}
}
document.getElementById("btn").onclick=function(){time(this);}
</script>
</body>
</html>
复制代码 代码如下:
<html>
<head>
<title>点击获取验证码按钮后按钮变灰,倒计时一段时间后又可重复点击</title>
</head>
<body>
<input type="button" id="btn" value="免费获取验证码" />
<script type="text/javascript">
var wait=60;
function time(o) {
if (wait == 0) {
o.removeAttribute("disabled");
o.value="免费获取验证码";
wait = 60;
} else {
o.setAttribute("disabled", true);
o.value=wait+"秒后可以重新发送";
wait--;
setTimeout(function() {
time(o)
},
1000)
}
}
document.getElementById("btn").onclick=function(){time(this);}
</script>
</body>
</html>
相关文章
重写document.write实现无阻塞加载js广告(补充)
这篇文章主要介绍了重写document.write实现无阻塞加载js广告,需要的朋友可以参考下2014-12-12开启Javascript中apply、call、bind的用法之旅模式
在Javascript中,Function是一种对象。Function对象中的this指向决定于函数被调用的方式,使用apply,call 与 bind 均可以改变函数对象中this的指向。2015-10-10
最新评论