Jquery发送验证码倒计时

Jquery发送验证码倒计时

Html

<input type="text" name="phone">
<input type="text" name="code" maxlength="6" placeholder="验证码">
<input type="button" id="yzm" value="获取验证码" />

Jquery

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)
    }
}

function  getcode( index )
{
    index.setAttribute("disabled", true);
    var phone = $("input[name='phone']").val();
    var reg = /^1[34578]\d{9}$/;
    if( !reg.test(phone) )
    {
        index.removeAttribute("disabled");
        $("input[name='phone']").focus();
    }else
    {
        $.post('您要验证的接口',{您要传递的参数},function( msg ){
            if( msg == 'success' )
            {
                time( index );
            }else
            {
                //这里处理你需要的业务
                index.removeAttribute("disabled");
            }
        },'json');
    }
}
document.getElementById("yzm").onclick=function(){getcode(this);}

相关文章

1.第一步 设置响应头 header(&#39;Access-Control-Allow...
$.inArray()方法介绍 $.inArray()函数用于在数组中搜索指定的...
jquery.serializejson.min.js的妙用 关于这个jquery.seriali...
JS 将form表单数据快速转化为object对象(json对象) jaymou...
jQuery插件之jquery.spinner数字智能增减插件 参考地址:http...