Jquery 插件防刷新 60秒倒计时
Posted 黄金时代1.0
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Jquery 插件防刷新 60秒倒计时相关的知识,希望对你有一定的参考价值。
//先到官网(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="http://apps.bdimg.com/libs/jquery/1.11.1/jquery.min.js" ></script> <script src="jquery.cookie.js" ></script> <style type="text/css"> * {margin: 0; padding: 0; font-family: "Microsoft Yahei";} .captcha-box {width: 360px; height: 34px; margin: 30px; padding: 30px; border: #956E6F 1px dashed; border-radius: 5px; background-color: #FAF2F2;} #mobile { float: left; width: 180px; height: 32px; border: #e5e5e5 1px solid; line-height: 32px; text-indent: 8px; outline: none;} #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;} </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>
我的应用实例
<script type="text/javascript"> $(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 myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/; var mobile = $("#phone").val(); alert(mobile); if(!myreg.test(mobile)) { alert(‘请输入有效的手机号码!‘); $("#phone").focus(); return false; } if(mobile==""){ alert("手机号码不为空"); $("#phone").focus(); return false; } var phone=$("#phone").val(); $.ajax({ type: "POST", url: ‘<%=basePath%>app/jiguang.do‘, data: {phone:phone}, dataType:‘json‘, cache: false, success: function(data){ if("success" == data.result){ alert(data.msg_id); $("#msg_id").val(data.msg_id) }else{ $("#phone").css("background-color","#D16E6C"); setTimeout("$(‘#phone‘).val(‘此用户名已存在!‘)",500); } } }); 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‘); }); });
百度云中保存 js文件 jquery.cookie.js
按照流程输入了正确信息却没有相应的显示效果,是因为: js文件发生了冲突 删掉报错的js以后项目完美运行
以上是关于Jquery 插件防刷新 60秒倒计时的主要内容,如果未能解决你的问题,请参考以下文章