参考:https://blog.csdn.net/JHON_03/article/details/76563212
为了用户体验度,增加了账号密码记录功能,本文为了使我加深印象使用,参考里写的更详细.
流程点击记住密码,下次登录输入用户名自动添加密码.
<input type="text" name="username" id="LAY-user-login-username" lay-verify="required" onblur="showpassword()" placeholder="用户名" class="layui-input">
<input type="password" name="password" id="LAY-user-login-password" lay-verify="required" placeholder="密码" class="layui-input">
<input type="checkbox" name="remember" id="remember" lay-skin="primary" title="记住密码">
登录成功后触发
//触发
function remember() {
if(document.getElementById("remember").checked){
SetCookie($("#LAY-user-login-username").val(),$("#LAY-user-login-password").val());
}
}
//保存
function SetCookie (name, value) {
var exp = new Date();
exp.setTime(exp.getTime() + (30*24*60*60*1000));
window.document.cookie = name + "=" + escape (value) + "; expires=" + exp.toGMTString()+";path=/";
}
//用户名输入完成自动读取cookie里密码
function showpassword() {
var p=GetCookie($("#LAY-user-login-username").val());
if(p!=null){
$("#LAY-user-login-password").val(p);
}
}
function GetCookie (name) {
var arg = name + "=";
var alen = arg.length;
var clen = window.document.cookie.length;
var i = 0;
while (i < clen)
{
var j = i + alen;
if (window.document.cookie.substring(i, j) == arg) return getCookieVal (j);
i = window.document.cookie.indexOf(" ", i) + 1;
if (i == 0)
break;
}
return null;
}
function getCookieVal (offset) {
var endstr = window.document.cookie.indexOf (";", offset);
if (endstr == -1)
endstr = window.document.cookie.length;
return unescape(window.document.cookie.substring(offset, endstr));
}
</script>