Home >Web Front-end >JS Tutorial >The specific implementation of JQuery to remember username and password_jquery

The specific implementation of JQuery to remember username and password_jquery

WBOY
WBOYOriginal
2016-05-16 16:53:131219browse
Copy code The code is as follows:






JQuery Code
Copy 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 username Password
function save() {
if ($("#ck_rmbUser").prop("checked")) {
var username = $("#username").val();
var password = $("#password").val();
$.cookie("rmbUser", "true", { expires: 7 }); //Storage a cookie with a 7-day expiry date
$.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 == "Please enter the username"){
$("#tip") .text("Please enter your username!");
$("#username").focus();
return false;
}
if(password == "" || password == "Please enter your password"){
$("#tip").text("Please enter your 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 username or password is wrong, please log in again! ");
return false;
}

}
});
}
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn