Javascript实现登录记住用户名和密码功能

话不多说,请看代码:

  <script type="text/javascript"> $(document).ready(function () { $("#UserAccount").focus(); //记住用户名和密码 $('#remebers').click(function () { if ($("#UserAccount").val() == "") { alert("用户名不能为空!"); } if($("#UserPassword").val() == "") { alert("密码不能为空!"); } else { if ($('#remebers').attr("checked")) { setCookie("uname", $("#UserAccount").val(), 60); setCookie("upwd", $("#UserPassword").val(), 60); } else { delCookie("uname"); delCookie("upwd"); } } }); if (getCookie("uname") != null) { $('#remebers').attr("checked", "checked"); $('#UserAccount').val(getCookie("uname")); $('#UserPassword').val(getCookie("upwd")); } }) //写cookies function setCookie(name, value) { var Days = 30; var exp = new Date(); exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000); document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString(); } //读取cookies function getCookie(name) { var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)"); if (arr = document.cookie.match(reg)) return unescape(arr[2]); else return null; } //删除cookies function delCookie(name) { var exp = new Date(); exp.setTime(exp.getTime() - 1); var cval = getCookie(name); if (cval != null) document.cookie = name + "=" + cval + ";expires=" + exp.toGMTString(); } </script>   <div> <section> @using (Html.BeginForm("Login", "Account", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post)) { @Html.AntiForgeryToken() @Html.ValidationSummary(true) <table> <tr> <td>账 号:</td> <td><input type="text" /> @Html.ValidationMessageFor(m => m.UserAccount)</td> </tr> <tr> <td>密 码:</td> <td> <input type="password" /> @Html.ValidationMessageFor(m => m.UserPassword) </td> </tr> <tr> <td></td> <td> <input type="checkbox" /> <span>记住用户名和密码</span> </td> </tr> <tr> <td></td> <td> <input type="submit" value="" /> &nbsp; <input type="reset" value="" /> </td> </tr> </table> } </section> <div> * 不要在公共场合保存登录信息;<br /> * 为了保证您的帐号安全,退出系统时请注销登录 <span></span> </div> </div>

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wwypgd.html