JQuery记住用户名密码实现下次自动登录功能
Jquery将用户名密码存储到cookie中
需要导入jquery.js和jquery.cookie.js
<html> <head> <title>testcookie</title> <scriptsrc="Scripts/jquery-1.4.1.min.js"type="text/javascript"></script> <scriptsrc="Scripts/jquery.cookie.js"type="text/javascript"></script> <scripttype="text/javascript"> $(document).ready(function(){ if($.cookie("rmbUser")=="true"){ $("#ck_rmbUser").attr("checked",true); $("#txt_username").val($.cookie("username")); $("#txt_password").val($.cookie("password")); } }); //记住用户名密码 functionSave(){ if($("#ck_rmbUser").attr("checked")){ varstr_username=$("#txt_username").val(); varstr_password=$("#txt_password").val(); $.cookie("rmbUser","true",{expires:7});//存储一个带7天期限的cookie $.cookie("username",str_username,{expires:7}); $.cookie("password",str_password,{expires:7}); } else{ $.cookie("rmbUser","false",{expire:-1}); $.cookie("username","",{expires:-1}); $.cookie("password","",{expires:-1}); } }; </script> </head> <body> <div> 用户名:<inputtype="text"id="txt_username"/><br/> 密码:<inputtype="text"id="txt_password"/><br/> <inputtype="checkbox"id="ck_rmbUser"/>记住用户名和密码<br/> <inputtype="submit"id="sub"value="登录"onclick="Save()"/> </div> </body> </html>