1. 從網上下載jquery.cookie.js,拷貝到應用程序中。
2. 登錄頁面(login.jsp)導入jquery.cookie.js
<script type="text/javascript">
$(function(){
$("#btn").click(function(){
var n = $("#username").val().trim();
var p = $("#password").val().trim();
if($('#cookiePwd').is(':checked')){
$('#cookiePwd').attr("checked",true);
$.cookie('username', n, {expires:7});
$.cookie('password', p, {expires:7});
//如果能匹配到說明是記住密碼,將復選框設置為選中狀態
}else{
$.cookie('username', null);
$.cookie('password', null);
}
var n = $.cookie('username');
var p = $.cookie('password');
//當賬號不為空的時候回顯賬號密碼,如果沒有密碼回顯的密碼也為空,
//當賬號為空的時候回顯兩個空沒意義
if(n){
$("#username").val(n);
$("#password").val(p);
}
});
$("#btn").attr({"type":"submit"});
});
</script>
<div class="login-wrapper">
<div class="login-area dib-m">
<form action="login.do" method="post" id="" name="" class="form">
<div id='btn_warning' class="bug-text" style="display: none;"></div>
<div class="user textbox"><input type="text" id="username" name="username" value="" placeholder="請輸入用戶名" ></div>
<div id='user_warning'class="bug-text"></div>
<div class="pass textbox"><input type="password" id="password" name="password" value="" placeholder="請輸入密碼"></div>
<div id='pass_warning' class="bug-text"></div>
<div class="btn">
<input type="button" class="login-btn dib-m" id="btn" value="登 錄">
<label class="dib-m">
<em class="c9">記住密碼</em>
<input type="checkbox" checked class="checkbox checkbox-animbg dib-m" id="cookiePwd" ></label>
</div>
</form>
</div>
<div class="dib-m h"></div>
</div>
