JS代码
function sendCode(btn){//获取手机号var phoneNum =document.getElementById(\"phoneId\").value;//后台Ajax请求$.ajax({type:\"post\",url: \"/toregister/send\",data: {phone: phoneNum},success: function(json){//console.log(json);alert(\"获取验证码成功\");}});time(btn);}//等待时间(测试时间为10秒)var wait = 10;function time(btn) {if (wait == 0) {btn.removeAttribute(\"disabled\");btn.innerHTML = \"免费获取验证码\";wait = 10;} else {btn.setAttribute(\"disabled\", true);btn.innerHTML = wait + \"秒后重新获取验证码\";console.log(wait + \"秒后重新获取验证码\");wait--;setTimeout(function () {time(btn);},1000)}}
按钮
//οnclick=\"sendCode(this),传入按钮自身对象this<button id=\"getCode\" class=\"btn-primary\"onclick=\"sendCode(this)\">发送短信验证码</button>