js短信验证码无视刷新

来源:互联网 发布:我的淘宝网首页登录 编辑:程序博客网 时间:2024/05/20 06:24
  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4. <meta charset="utf-8">  
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">  
  6. <title>Examples</title>  
  7. <meta name="description" content="">  
  8. <meta name="keywords" content="">  
  9. <script src="http://apps.bdimg.com/libs/jquery/1.11.1/jquery.min.js" ></script>  
  10. <script src="jquery.cookie.js" ></script>  
  11. <style type="text/css">  
  12.     * {margin: 0; padding: 0; font-family: "Microsoft Yahei";}  
  13.     .captcha-box {width: 360px; height: 34px; margin: 30px; padding: 30px; border: #956E6F 1px dashed; border-radius: 5px; background-color: #FAF2F2;}  
  14.     #mobile { float: left; width: 180px; height: 32px; border: #e5e5e5 1px solid; line-height: 32px; text-indent: 8px; outline: none;}  
  15.     #getting {float: left; height: 34px; margin-left: -1px; padding: 0 18px; text-align: center;  line-height: 34px; border: #e5e5e5 1px solid; background: linear-gradient(0deg, #f4f2f2 0%,#fbf9f9 100%); cursor: pointer; outline: none;}  
  16. </style>  
  17. <script>  
  18.     $(function(){  
  19.    
  20.         /*仿刷新:检测是否存在cookie*/  
  21.         if($.cookie("captcha")){  
  22.             var count = $.cookie("captcha");  
  23.             var btn = $('#getting');  
  24.             btn.val(count+'秒后可重新获取').attr('disabled',true).css('cursor','not-allowed');  
  25.             var resend = setInterval(function(){  
  26.                 count--;  
  27.                 if (count > 0){  
  28.                     btn.val(count+'秒后可重新获取').attr('disabled',true).css('cursor','not-allowed');  
  29.                     $.cookie("captcha", count, {path: '/', expires: (1/86400)*count});  
  30.                 }else {  
  31.                     clearInterval(resend);  
  32.                     btn.val("获取验证码").removeClass('disabled').removeAttr('disabled style');  
  33.                 }  
  34.             }, 1000);  
  35.         }  
  36.    
  37.         /*点击改变按钮状态,已经简略掉ajax发送短信验证的代码*/  
  38.         $('#getting').click(function(){  
  39.             var btn = $(this);  
  40.             var count = 60;  
  41.             var resend = setInterval(function(){  
  42.                 count--;  
  43.                 if (count > 0){  
  44.                     btn.val(count+"秒后可重新获取");  
  45.                     $.cookie("captcha", count, {path: '/', expires: (1/86400)*count});  
  46.                 }else {  
  47.                     clearInterval(resend);  
  48.                     btn.val("获取验证码").removeAttr('disabled style');  
  49.                 }  
  50.             }, 1000);  
  51.             btn.attr('disabled',true).css('cursor','not-allowed');  
  52.         });  
  53.    
  54.     });  
  55. </script>  
  56. </head>  
  57. <body>  
  58.     <div class="captcha-box">  
  59.         <input type="text" id="mobile" maxlength="11" placeholder="请输入手机号码">  
  60.         <input type="button" id="getting" value="获取验证码">  
  61.     </div>  
  62. </body>  
  63. </html>  
原创粉丝点击