jquery 实现 点击按钮后倒计时效果,多用于实现发送手机验证码

<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>
    </title>
    <script src="Scripts/jquery-2.1.1.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        var InterValObj; //timer变量,控制时间
        var count = 60; //间隔函数,1秒执行
        var curCount;//当前剩余秒数\
        var code = ""; //验证码
        var codeLength = 6;//验证码长度
        function sendMessage() {
            curCount = count;
            var dealType = $("#phones").val(); //手机号码
            var uid = $("#uid").val();//用户uid
            
            //产生验证码
            for (var i = 0; i < codeLength; i++) {
                code += parseInt(Math.random() * 9).toString();
            }
            //设置button效果,开始计时
            $("#btnSendCode").attr("disabled", "true");
            $("#btnSendCode").val("请在" + curCount + "秒内输入验证码");
            InterValObj = window.setInterval(SetRemainTime, 1000); //启动计时器,1秒执行一次
            //向后台发送处理数据
            $.ajax({
                type: "POST", //用POST方式传输
                dataType: "text", //数据格式:JSON
                url: Admins/Index6, //目标地址
                data: "dealType=" + dealType + "&uid=" + uid + "&code=" + code,
                error: function (XMLHttpRequest, textStatus, errorThrown) { },//错误处理
                success: function (msg) {  }//返回内容
            });
            
        }
  
        //timer处理函数
        function SetRemainTime() {
            if (curCount == 0) {
                window.clearInterval(InterValObj);//停止计时器
                $("#btnSendCode").removeAttr("disabled");//启用按钮
                $("#btnSendCode").val("重新发送验证码");
                code = ""; //清除验证码。如果不清除,过时间后,输入收到的验证码依然有效
            }
            else {
                //冷却60秒按钮可点击
                curCount--;
                $("#btnSendCode").val("请在" +     curCount + "秒内输入验证码");
            }
        }
    </script>
    
</head>
<body>
   
    验证码<img id="imaCode2" src="ValidCode.ashx" onclick="this.src=‘ValidCode.ashx?number=‘+Math.random()" />
    <p>手机号:<input id="phones" type="text" value="" /> </p>
    <P>用户名:<input id="uid" type="text" value=""  /> </P>
    <P><input id="btnSendCode" type="button" value="发送验证码" onclick="sendMessage()" /></p>
</body>
</html>

 控制器下的代码

       [HttpPost]
        public ActionResult Index6()
        {
            string zh ="XXXXXXX";//短信接口账户
            string mm = "123456";//账户密码
            int sms_type = 42; //通道编号
            string dealType = Request["dealType"];//电话号码
            string uid = Request["uid"];//用户ID
            string code = Request["code"];//随机数
//内容 string nr = "您好用户:" + uid + ",你的验证码为:" + code + "请尽快输入!";
//短信接口通道地址如:
string url = "http://www.6610086.net/jk.aspx?zh=" + zh + "8&mm=" + mm + "&hm=" + dealType + "&nr=" + nr + "&sms_type=" + sms_type; Response.Redirect(url); // Response.Write("<script>$.ajax({type: ‘POST‘, dataType: ‘text‘, url: " + url + ",data:null,error: function (XMLHttpRequest, textStatus, errorThrown) { },success: function (msg) { }});</script>"); //Response.Write("<script>$.ajax([type:‘POST‘,dataType:‘text‘,]);</script>"); return View(); }

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。