近來做記住密碼時,用js的實現方式做了一下。
login.jsp頁面代碼
- <%@ 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 XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
- <head>
- <base href="<%=basePath%>" />
- <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
- <link rel="stylesheet" href="./css/login.css" />
- <title>登錄</title>
- <!-- 引入js-->
- <script src="./js/lib/jquery-1.8.0.min.js" type="text/javascript" ></script>
- <script src="./js/backjs/login.js" type="text/javascript" ></script>
- </head>
- <body>
- <div class="warp">
- <img src="./images/bf.png" alt="" />
- <div class="window">
- <div class="loginWindow"></div>
- <div class="loginWindowContent">
- <form action="#" id="loginForm">
- <label for="" class="weicome">歡迎登錄xxx系統</label>
- <label for="" class="slogan">智能隨行 隨到隨停</label>
- <label for="" class="input"> <i> <span></span></i>
- <input type="text" placeholder="請輸入用戶名/手機號碼" name="name" id="name" />
- </label>
- <label for="" class="input" style="margin-top:30px;"> <i><span style="background: url(./images/lock.png) no-repeat;"></span></i>
- <input type="password" placeholder="請輸入密碼" name="password" id="password"/>
- </label>
- <label for="" class="savePass">
- <input type="checkbox" value="savePW" name="savePW" id="savePW"/>記住密碼 <a href="home/resetPwdUI">忘記登錄密碼?</a>
- </label>
- <label for="" class="sub">
- <p style="color: red" id="loginMes"></p>
- <input type="button" value="登錄" id="login_BTN"/>
- </label>
- <label for="" class="reg">還不是智停車會員,<a href="home/registerUI"><b>點擊免費注冊</b>>></a></label>
- </form>
- </div>
- </div>
- <div class="footer">
- <div class="about">
- <b><a href="">關於xxx</b>
- </div>
- </div>
- </div>
- </body>
- </html>
login.js文件
- /**
- * 登錄js
- */
- $().ready(function(){
- var loginSum = 0;
- //點擊登錄按鈕事件
- $("#login_BTN").unbind("click");
- $("#login_BTN").bind("click",function(){
- //修改頁面消息顯示
- $("#loginMes").text("正在登陸。。。");
- //獲取表單數據
- var data = $("#loginForm");
- var jsonData = data.serialize();
- var name = data.find("input[name='name']").val();
- var password = data.find("input[name='password']").val();
- var savePW = data.find("input[name='savePW']").val();
- if(name == '' || name == null){
- $("#loginMes").text("用戶名不能為空");
- return;
- };
- if(password == '' || password == null){
- $("#loginMes").text("密碼不能為空");
- return;
- };
- //判斷重復提交
- if(loginSum == 1){
- //重復提交
- $("#loginMes").text("正在登陸,請稍候");
- return;
- };
- if(loginSum == 0){
- loginSum = 1;
- };
- //提交
- $.ajax({
- url:"home/login",
- type:'POST',
- data:jsonData,
- dataType:'json',
- error:function(){
- alert("出錯了!!");
- loginSum = 0;
- },
- success:function (data){
- if(data.code == 0){
- //保存密碼
- SetPwdAndChk();
- $("#loginMes").text("登錄成功,正在跳轉。。。");
- window.location.href="main/home";
- }else{
- $("#loginMes").text("登錄失敗,用戶名或密碼錯誤!");
- loginSum = 0;
- };
- }
- });
- });
- //用戶名失去焦點時調用該方法
- $("#name").unbind("blur");
- $("#name").bind("blur",function(){
- GetPwdAndChk();
- });
- //=============js cookie===============
- GetLastUser();
- function GetLastUser() {
- var id = "49BAC005-7D5B-4231-8CEA-16939BEACD67";
- var usr = GetCookie(id);
- if (usr != null) {
- document.getElementById('name').value = usr;
- } else {
- //document.getElementById('name').value = "001";
- }
- GetPwdAndChk();
- };
- //點擊登錄時觸發客戶端事件
- function SetPwdAndChk() {
- //取用戶名
- var usr = document.getElementById('name').value;
- //alert(usr);
- //將最后一個用戶信息寫入到Cookie
- SetLastUser(usr);
- //如果記住密碼選項被選中
- var checked = document.getElementById('savePW').checked;
- if (checked == true) {
- //取密碼值
- var pwd = document.getElementById('password').value;
- //alert(pwd);
- var expdate = new Date();
- expdate.setTime(expdate.getTime() + 14 * (24 * 60 * 60 * 1000));
- //將用戶名和密碼寫入到Cookie
- SetCookie(usr, pwd, expdate);
- } else {
- //如果沒有選中記住密碼,則立即過期
- ResetCookie();
- }
- };
- function SetLastUser(usr) {
- var id = "49BAC005-7D5B-4231-8CEA-16939BEACD67";
- var expdate = new Date();
- //當前時間加上兩周的時間
- expdate.setTime(expdate.getTime() + 14 * (24 * 60 * 60 * 1000));
- SetCookie(id, usr, expdate);
- };
- //用戶名失去焦點時調用該方法
- function GetPwdAndChk() {
- var usr = document.getElementById('name').value;
- var pwd = GetCookie(usr);
- if (pwd != null) {
- document.getElementById('savePW').checked = true;
- document.getElementById('password').value = pwd;
- } else {
- document.getElementById('savePW').checked = false;
- document.getElementById('password').value = "";
- }
- };
- //取Cookie的值
- function GetCookie(name) {
- var arg = name + "=";
- var alen = arg.length;
- var clen = document.cookie.length;
- var i = 0;
- while (i < clen) {
- var j = i + alen;
- //alert(j);
- if (document.cookie.substring(i, j) == arg)
- return getCookieVal(j);
- i = document.cookie.indexOf(" ", i) + 1;
- if (i == 0)
- break;
- }
- return null;
- };
- function getCookieVal(offset) {
- var endstr = document.cookie.indexOf(";", offset);
- if (endstr == -1)
- endstr = document.cookie.length;
- return unescape(document.cookie.substring(offset, endstr));
- };
- //寫入到Cookie
- function SetCookie(name, value, expires) {
- var argv = SetCookie.arguments;
- //本例中length = 3
- var argc = SetCookie.arguments.length;
- var expires = (argc > 2) ? argv[2] : null;
- var path = (argc > 3) ? argv[3] : null;
- var domain = (argc > 4) ? argv[4] : null;
- var secure = (argc > 5) ? argv[5] : false;
- document.cookie = name
- + "="
- + escape(value)
- + ((expires == null) ? "" : ("; expires=" + expires
- .toGMTString()))
- + ((path == null) ? "" : ("; path=" + path))
- + ((domain == null) ? "" : ("; domain=" + domain))
- + ((secure == true) ? "; secure" : "");
- };
- function ResetCookie() {
- var usr = document.getElementById('name').value;
- var expdate = new Date();
- SetCookie(usr, null, expdate);
- };
- });
- 轉自:http://blog.csdn.net/u013614451/article/details/42201799