<html>  
    <head>  
    <title>test cookie</title> 
    <meta charset="UTF-8"/>
    <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
    <script type="text/javascript" src="jquery.cookie.js">
    //需要引入jquery.cookie.js和jquery.min.js
    </script>
    <script type="text/javascript">  
      
        $(document).ready(function () {  
            if ($.cookie("rmbUser") == "true") {  
            $("#ck_rmbUser").attr("checked", true);  
            $("#txt_username").val($.cookie("username"));  
            $("#txt_password").val($.cookie("password"));  
            }  
        });  
      
        //记住用户名密码  
        function Save() {      
            if ($("#ck_rmbUser").is(":checked")) { 
                var str_username = $("#txt_username").val();  
                var str_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 });  
            }  
        };  
        
       function ma(){
          if($("#ck_rmbUser").is(":checked")==false){
               
                $.cookie("rmbUser", "false", { expire: -1 });  
                $.cookie("username", "", { expires: -1 });  
                $.cookie("password", "", { expires: -1 }); 
                $("#ck_rmbUser").attr("checked", false);  
                $("#txt_username").val($.cookie("username"));  
                $("#txt_password").val($.cookie("password")); 
          }
       }
    </script>  
    </head>  
    <body>  
        <div>  
            用户名:<input type="text" id="txt_username"/><br />  
            密码:<input type="text" id="txt_password"/><br />  
            <input type="checkbox" id="ck_rmbUser" οnclick="ma()"/>记住用户名和密码<br />  
            <input type="submit" id="sub" value="登录" οnclick="Save()"/>  
        </div>  
    </body>  
    </html> 

jquery通过cookies记住用户名和密码

发表评论 已发布 0

还可以输入 800 个字
 
 
打印