本地保存cookie
原文https://blog.csdn.net/qq_29663071/article/details/73826642
https://www.cnblogs.com/webcome/p/5470975.html
<!DOCTYPE html>
<html lang="en">
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" href="/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<div class="row" style="margin-top: 200px">
<div class="col-sm-6 col-lg-offset-2">
<form method="post" class="form-horizontal">
<div class="form-group">
<label for="account" class="col-sm-2 control-label">账户</label>
<div class="col-sm-10">
<input id="account" name="account" class="form-control" th:placeholder="请输入账户"></input>
</div>
</div>
<div class="form-group">
<label for="password" class="col-sm-2 control-label">密码</label>
<div class="col-sm-10">
<input id="password" type="text" name="password" th:placeholder="请输入密码" class="form-control">
</div>
</div>
<div class="form-group">
<div class="col-sm-10 col-lg-offset-2">
<div class="checkbox">
<label>
<input type="checkbox" id="checkbox">请记住我
</label>
</div>
</div>
</div>
<div class="form-group">
<div class="col-sm-3 col-lg-offset-5">
<input id="button" type="button" class="form-control btn btn-primary" value="登录">
</div>
</div>
</form>
</div>
</div>
</div>
<script src="/js/bootstrap.min.js"></script>
<script src="/js/jquery.js"></script>
<script src="/js/jquery.cookie.js"></script>
<script>
if ($.cookie('checkbox')=="true") {
var checkflg = true;
$("#checkbox").attr("checked", true);
$("#account").val($.cookie("account"));
$("#password").val($.cookie("password"));
}else{
// 默认不记住
var checkflg = false;
}
$("#checkbox").click(function () {
if (!checkflg){
$("#checkbox").attr("checked",true);
}else{
$("#checkbox").attr("checked",false);
}
checkflg = !checkflg;
});
function setCookie() {
if (checkflg) {
var account = $("#account").val();
var password = $("#password").val();
$.cookie("checkbox", "true", { expires: 7 });
$.cookie("account", account, { expires: 7 });
$.cookie("password", password, { expires: 7 });
} else {
$.cookie("checkbox", "false", { expires: -1 });
$.cookie("account", '', { expires: -1 });
$.cookie("password", '', { expires: -1 });
}
}
$("#button").click(function () {
setCookie();
window.location.href="/login/loginValidate?account="+$("#account").val()+"&password="+$("#password").val();
});
</script>
</body>
</html>