用cookie保存用戶的登錄信息,規定保存的期限


html:

        <form action="#" method="get" id="mycookie">
          <table border="0" cellspacing="0" cellpadding="0">
            <tbody>
              <tr height="50" valign="top">
                <td width="55">&nbsp;</td>
                <td>
                  <span class="fl" style="font-size:24px;">登錄</span>
                  <span class="fr">還沒有商城賬號,<a href="Regist.html" style="color:#ff4e00;">立即注冊</a></span>
                </td>
              </tr>
              <tr height="70">
                <td>用戶名</td>
                <td><input id="name" type="text" value="" class="l_user"></td>
              </tr>
              <tr height="70">
                <td>密&nbsp; &nbsp; 碼</td>
                <td><input id="pasw" type="password" value="" class="l_pwd"></td>
              </tr>
              <tr>
                <td>&nbsp;</td>
                <td style="font-size:12px; padding-top:20px;">
                  <span style="font-family:'宋體';" class="fl">
                    <label class="r_rad"><input id="chkSave" type="checkbox"></label><label
                      class="r_txt">請保存我這次的登錄信息</label>
                  </span>
                  <span class="fr"><a href="javascript:void(0)" style="color:#ff4e00;">忘記密碼</a></span>
                </td>
              </tr>
              <tr height="60">
                <td>&nbsp;</td>
                <td><input type="submit" value="登錄" class="log_btn"></td>
              </tr>
            </tbody>
          </table>
        </form>

javascript:

    $(function () {
            //保存客戶的登錄信息
            if ($.cookie("name") && $.cookie("pasw")) {
              //取值如果存在則賦值 
              $("#name").val($.cookie("name"));
              $("#pasw").val($.cookie("pasw"))
            }
            $("#mycookie").submit(function () {
              //如果選中了保存用戶名選項 
              if ($("#chkSave").is(":checked")) {
                //設置Cookie值 
                $.cookie("name", $("#name").val(), {
                  expires: 7,//設置保存期限  7天
                  path: "/"//設置保存的路徑 
                });
                $.cookie("pasw", $("#pasw").val(), {
                  expires: 7,//設置保存期限  7天
                  path: "/"//設置保存的路徑 
                });
                alert("存了cookie");
              }
              else {
                //銷毀對象 
                $.cookie("name", null, {
                  path: "/"
                });
                $.cookie("pasw", null, {
                  path: "/"
                });
                alert("銷毀了cookie");
              }
         
              return false;
            });
          })

4.備注: cookie只有在啟動服務器項目下,才能進行正常存取操作。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM