js点击记住账号实现记住用户名和密码

用js写,点击记住账号,登录过一次然后回到登录页再次登录的时候自动保存账户名和密码
怎么用cookie存

如图所示操作代码即可:

登录页面实现记住用户名和密码功能的方法:

1、使用cookie的方法将用户名和密码保存到本地电脑上;

2、PS在页面载入时获取cookie;

3、在勾选时设置一个cookie的有效时间就可以了。

温馨提示:答案为网友推荐,仅供参考
第1个回答  2015-11-03
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    账号:<input>
    密码:<input>
    记住密码:<input type="checkbox">
    <button onclick="fn()">登录</button>
<script>
    var user = document.getElementsByTagName("input")[0],
            pass = document.getElementsByTagName("input")[1],
            check = document.getElementsByTagName("input")[2],
            loUser = localStorage.getItem("user") || "";
            loPass = localStorage.getItem("pass") || "";
        user.value = loUser;
        pass.value = loPass;
        if(loUser !== "" && loPass !== ""){
            check.setAttribute("checked","");
        }
    function fn(){
        if(check.checked){
            localStorage.setItem("user",user.value);
            localStorage.setItem("pass",pass.value);
        }else{
            localStorage.setItem("user","");
            localStorage.setItem("pass","");
        }
    }
</script>
</body>
</html>

本回答被网友采纳
相似回答