点击“获取验证码”然后出现一个“倒计时”然后“获取验证码”变灰,jQuery

点击“获取验证码”然后出现一个“倒计时”然后“获取验证码”变灰,jQuery,第1张

先到官网(http://plugins.jquery.com/cookie/)下载cookie插件,放到相应文件夹,代码如下:

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">

<title>Examples</title>

<meta name="description" content="">

<meta name="keywords" content="">

<script src="<a href="http://apps.bdimg.com/libs/jquery/1.11.1/jquery.min.js">http://apps.bdimg.com/libs/jquery/1.11.1/jquery.min.js</a>" ></script>

<script src="jquery.cookie.js" ></script>

<style type="text/css">

* {margin: 0padding: 0font-family: "Microsoft Yahei"}

.captcha-box {width: 360pxheight: 34pxmargin: 30pxpadding: 30pxborder: #956E6F 1px dashedborder-radius: 5pxbackground-color: #FAF2F2}

#mobile { float: leftwidth: 180pxheight: 32pxborder: #e5e5e5 1px solidline-height: 32pxtext-indent: 8pxoutline: none}

#getting {float: leftheight: 34pxmargin-left: -1pxpadding: 0 18pxtext-align: center line-height: 34pxborder: #e5e5e5 1px solidbackground: linear-gradient(0deg, #f4f2f2 0%,#fbf9f9 100%)cursor: pointeroutline: none}

</style>

<script>

$(function(){

/*仿刷新:检测是否存在cookie*/

if($.cookie("captcha")){

var count = $.cookie("captcha")

var btn = $('#getting')

btn.val(count+'秒后可重新获取').attr('disabled',true).css('cursor','not-allowed')

var resend = setInterval(function(){

count--

if (count >0){

btn.val(count+'秒后可重新获取').attr('disabled',true).css('cursor','not-allowed')

$.cookie("captcha", count, {path: '/', expires: (1/86400)*count})

}else {

clearInterval(resend)

btn.val("获取验证码").removeClass('disabled').removeAttr('disabled style')

}

}, 1000)

}

/*点击改变按钮状态,已经简略掉ajax发送短信验证的代码*/

$('#getting').click(function(){

var btn = $(this)

var count = 60

var resend = setInterval(function(){

count--

if (count >0){

btn.val(count+"秒后可重新获取")

$.cookie("captcha", count, {path: '/', expires: (1/86400)*count})

}else {

clearInterval(resend)

btn.val("获取验证码").removeAttr('disabled style')

}

}, 1000)

btn.attr('disabled',true).css('cursor','not-allowed')

})

})

</script>

</head>

<body>

<div class="captcha-box">

<input type="text" id="mobile" maxlength="11" placeholder="请输入手机号码">

<input type="button" id="getting" value="获取验证码">

</div>

</body>

</html>

参考下

之前弄过的稍微改了下,你看看是不是这样。

整个Form就加了个Button控件,改名“获取验证”。

 public partial class Form1 : Form

    {

        System.Timers.Timer reGetTimer = new System.Timers.Timer() 

        public static int Times = 1

        public Form1()

        {

            InitializeComponent()

            reGetTimer.Elapsed += ReGetTimer_Elapsed

            reGetTimer.Interval = 1000

            reGetTimer.Enabled = false

        }

        private void ReGetTimer_Elapsed(object sender, ElapsedEventArgs e)

        {

            if (Times <= 10)

            {

                this.Invoke(new EventHandler(delegate { 获取验证.Text = Times.ToString()}))

                Times++

            }

            else

            {

                reGetTimer.Enabled = false

                Times = 1

                this.Invoke(new EventHandler(delegate { 获取验证.Text = "重新获取" }))

            }

        }

        private void 获取验证_Click(object sender, EventArgs e)

        {

            reGetTimer.Enabled = true 

        }

 }


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/7986848.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-12
下一篇 2023-04-12

发表评论

登录后才能评论

评论列表(0条)

保存