jquery 登录-记住密码
在登录时,添加记住密码功能:
用的 localStorage 存储和获取登录信息
//存储
var username = $("input[name='username']").val();
var password = $("input[name='password']").val();
var validateCode = $("input[name='validateCode']").val();
var rememberMe = $("input[name='rememberme']").is(':checked');
$.ajax({
type: "post",
url:"/login",
data: {
"username": username,
"password": password,
"rememberMe": rememberMe
},
success: function (r) {
if (r.code == web_status.SUCCESS) {
//跳转到首页
location.href = ctx + 'index';
//账号密码存到本地
localStorage.setItem("username",username)
localStorage.setItem("password",password)
localStorage.setItem("rememberMe",rememberMe)
}
else {
$.modal.closeLoading();
$('.imgcode').click();
$(".code").val("");
$.modal.msg(r.msg);
}
}
})
//获取
var username=localStorage.getItem("username")
var password=localStorage.getItem("password")
var rememberMe=localStorage.getItem("rememberMe")
if(rememberMe == 'true'){//记住密码
$("input[name='username']").val(username)
$("input[name='password']").val(password)
$("#rememberme").prop("checked",true)
}else{
$("input[name='username']").val('')
$("input[name='password']").val('')
$("#rememberme").prop("checked",false)
}