发送验证码(××s后重新发送)
html:
<input class="tel" type="text" name="tel" placeholder="手机号"> <a class="yz" href="javascript:;" onclick="getCode(this,60)">验证</a>
JS:
var iphonecode='';//接受验证码 function getCode(obj,time){ var _time = time || 60; var _phoneval = $(obj).siblings('input').val(); var _phonetest = /^1[34578]\d{9}$/; var _fun = $(obj).attr('onclick'); var flag = _phonetest.test(_phoneval); if(flag){ $(obj).addClass('on'); var s = setInterval(function(){ _time --; if(_time > 0){ $(obj).html(_time+'s后获取').attr('onclick',''); }else{ clearInterval(s); $(obj).attr('onclick',_fun).html('再次获取').removeClass('on'); } },1000); // 请用 iphonecode 接受返回的验证码 iphonecode = 123456; }else{ layer.tips('手机号码格式不正确', obj); } }