This article mainly introduces JQuery's method of remembering the user name and password. For more information, see
The Code is as follows:
JQuery Code
The Code is as follows:
$ (Document). ready (function (){
If ($. cookie ("rmbUser") = "true "){
$ ("# Ck_rmbUser"). prop ("checked", true );
$ ("# Username"). val ($. cookie ("username "));
$ ("# Password"). remove ();
$ ("# Pass"). append ("");
$ ("# Password"). val ($. cookie ("password "));
}
$ ("# LoginButton"). click (function (){
If (check ()){
Login ();
}
});
});
// Remember the username and password
Function save (){
If ($ ("# ck_rmbUser"). prop ("checked ")){
Var username = $ ("# username"). val ();
Var password = $ ("# password"). val ();
$. Cookie ("rmbUser", "true", {expires: 7}); // store a cookie with a 7-day validity period
$. Cookie ("username", username, {expires: 7 });
$. Cookie ("password", password, {expires: 7 });
} Else {
$. Cookie ("rmbUser", "false", {expire:-1 });
$. Cookie ("username", "", {expires:-1 });
$. Cookie ("password", "", {expires:-1 });
}
};
Function check (){
Var username = $ ("# username"). val ();
Var password = $ ("# password"). val ();
If (username = "" | username = "Enter the user name "){
$ ("# Tip"). text ("Enter the user name! ");
$ ("# Username"). focus ();
Return false;
}
If (password = "" | password = "enter the password "){
$ ("# Tip"). text ("enter the password! ");
$ ("# 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 ("the user name or password is incorrect. Please log on again! ");
Return false;
}
}
});
}