一个简单的前端获取手机验证码实现

//获取验证码 
var wait = 30; 
var send = true; 
function getCaptcha(o) { 
   if (0 == wait) {
      o.removeAttribute("disabled"); 
      o.value = "获取验证码"; 
      wait = 30; 
      send = true; 
    } else {
    if (send) { 
      var phoneno = $("#phoneno").val();
       if (!checkPhone(phoneno)) { 
           alert("请输入有效的手机号码"); 
           return;
       if (phoneno == $('#bindpage-main .old_num').attr('data')) {
                      alert("不能与原手机号码相同"); 
                      return; 
               }
              var url = url_base + "/captcha/phone?phoneno=" + phoneno;
              var request = $.ajax({ url: url, method: "GET", dataType: 'text' });
              request.done(function (msg) { $("#captcha_temp").val(msg); }); 
              request.fail(function (jqXHR, textStatus) { 
                        //alert("Request failed: " + textStatus); 
                       alert('出错了'); }); 
      send = false; 

     o.setAttribute("disabled", true); 
     o.value = "重新发送(" + wait + ")"; 
     wait--; 
      setTimeout(function () { 
            getCaptcha(o);
       }, 1000);
}

 
 

你可能感兴趣的:(微服务,JavaScript)