jquery简单实现记住密码

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
 String path = request.getContextPath();
 String basePath = request.getScheme() + "://"
   + request.getServerName() + ":" + request.getServerPort()
   + path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<%--<script type="text/javascript" src="jslib/jquery-1.8.3.min.js"></script>
--%>
<script type="text/javascript" src="jslib/jquery-1.9.1.min.js"></script>
<script type="text/javascript" src="jslib/jquery.cookie.js"></script>
<script type="text/javascript">
 //初始化页面时验证是否记住了密码
 $(document).ready(function() {
  alert("用户名:" + $.cookie("userName") + " 密码:" + $.cookie("passWord"));
  if ($.cookie("rmbUser") == "true") {
   $("#rmbUser").attr("checked", true);
   $("#user").val($.cookie("userName"));
   $("#pass").val($.cookie("passWord"));
  }
 });
 //保存用户信息
 function saveUserInfo() {
  if ($("#rmbUser").attr("checked") == "checked") {
   var userName = $("#user").val();
   var passWord = $("#pass").val();
   $.cookie("rmbUser", "true", {
    expires : 7
   }); // 存储一个带7天期限的 cookie 
   $.cookie("userName", userName, {
    expires : 7
   }); // 存储一个带7天期限的 cookie 
   $.cookie("passWord", passWord, {
    expires : 7
   }); // 存储一个带7天期限的 cookie 
  } else {
   $.cookie("rmbUser", "false", {
    expires : -1
   });
   $.cookie("userName", '', {
    expires : -1
   });
   $.cookie("passWord", '', {
    expires : -1
   });
  }
 }
</script>
</head>
<body>
 <div>
  <form action="" method="post">
   <table>
    <tr>
     <th>用户名:</th>
     <td><input type="text" id="user" /></td>
    </tr>
    <tr>
     <th>密码:</th>
     <td><input type="password" id="pass" /></td>
    </tr>
    <tr>
     <th>记住密码</th>
     <td><input type="checkbox" id="rmbUser" /><input
      type="submit" value="提交" onclick="saveUserInfo()" /></td>
    </tr>
   </table>
  </form>
 </div>
</body>
</html>

相关推荐