<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>倒计时10s</title> <script type="text/javascript" src="js/jquery-2.1.4.min.js"></script> </head> <body> 请输入验证码:<input type="text" /> <span id="checkCode"></span> <input type="button" id="btn" value="免费获取验证码" onclick="settime(this)" /> <script type="text/javascript"> var countdown=10;//设置多久后从新发送 function settime(val) { if (countdown == 0) { val.removeAttribute("disabled"); val.value="免费获取验证码"; countdown = 10; // window.location.href='https://www.baidu.com/' } else { val.setAttribute("disabled", true); val.value="从新发送(" + countdown + ")"; countdown--; setTimeout(function() { settime(val) },1000) } } //当用户点击了一个按钮 先将验证码追加到位置 $(function(){ $("#btn").click(function(){ var num=""; for(var i=0;i<4;i++){ num+=Math.floor(Math.random()*10) } //将内容追加在验证码位置 $("#checkCode").html(num); alert("后台动态生成验证码") }); }); </script>html </body> </html>java |