<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>记住密码</title>
</head>
<body>
<label>用户名</label> <input type="text" id="user" value="" /><br />
<label>密码</label> <input type="password" id="pas" value="" /><br />
<label> <input type="checkbox" id="sed" value="" />记住密码</label>
<button type="button" id="btn">提交</button>
</body>
</html>
<script type="text/javascript">
function $(ele) {
return document.querySelector(ele);
}
//加载的时候自动输入用户名
if (localStorage.getItem('user')) {
$('#user').value = localStorage.getItem('user');
}
if (localStorage.getItem('pas')) {
$('#pas').value = localStorage.getItem('pas');
$('#sed').checked = true;
}
$('#btn').onclick = function () {
localStorage.setItem('user', $('#user').value);
// 首先判断记住密码是否勾选
console.log($('#sed').checked);
if ($('#sed').checked) {
localStorage.setItem('pas', $('#pas').value);
} else {
localStorage.removeItem('pas');
}
};
</script>
js 记住密码(localstorage)
最新推荐文章于 2024-04-27 06:46:18 发布