1,html部分
2,jquery实现记住用户名和密码的代码。
$(document).ready(function(){
if ($.cookie("rmbuser") == "true") {
$("#ck_rmbuser").prop("checked", true);
$("#username").val($.cookie("username"));
$("#password").remove();
$("#pass").append("<input id='password' type='password' class='txt2'/>");
$("#password").val($.cookie("password"));
}
$("#loginbutton").click(function(){
if(check()){
login();
}
});
});
//记住用户名密码
function save() {
if ($("#ck_rmbuser").prop("checked")) {
var username = $("#username").val();
var password = $("#password").val();
$.cookie("rmbuser", "true", { expires: 7 }); //存储一个带7天期限的cookie
$.cookie("username", username, { expires: 7 });
$.cookie("password", password, { expires: 7 });
}else{
$.cookie("rmbuser", "false", { expire: -1 });
$.cookie("username", "", { expires: -1 });
$.cookie("password", "", { expires: -1 });
} // 脚本学堂 www.jb200.com
};
function check(){
var username = $("#username").val();
var password = $("#password").val();
if(username == "" || username == "请输入用户名"){
$("#tip").text("请输入用户名!");
$("#username").focus();
return false;
}
if(password == "" || password == "请输入密码"){
$("#tip").text("请输入密码!");
$("#password").focus();
return false;
}
$("#tip").text("");
return true;
}
function login(){
$.ajax({
type:"post",
url: "login!loginvalidate.action",
data:{username:$("#username").val(),password:$("#password").val()},
datatype:"json",
beforesend: function(){
showoverlay();
},
success:function(data){
if(data.success){
addcookie("username", $("#username").val(), 0);
save();
location.href = "/index.jsp";
}else{
$("#overlay").hide();
$("#tip").text("用户名或密码错误,请重新登录!");
return false;
}
}
});
}