账号:<input type="text" id="username" /> 密码: <input type="password" id="pwd" /> <input type="checkbox" name="check" id="check" value=""/> 记住账号密码 <input type="button" name="" id="login" value="登录" />
<script> var oDate = new Date("2016/3/1,9:30:00"); document.cookie = 'username=ketty;expires='+oDate; var btn = document.getElementById("login"); var ocheck = document.getElementById("check") var uer = document.getElementById("username"); var pwd = document.getElementById("pwd"); btn.onclick = function(){ // console.log(ocheck.checked)//false 或者true var oDate = new Date("2016/3/1,10:30:00"); if(ocheck.checked) { document.cookie = "username="+uer.value+"expires="+oDate; document.cookie= "password="+pwd.value+"expires="+oDate; } } // password=123123213123123expiresexpires=Tue Mar 01 2016 10:30:00 GMT+0800 (中国标准时间); username=ketty console.log(document.cookie); var reg =/s+/; var cookies = document.cookie.replace(reg,""); var cookiesArr = cookies.split(";"); for(var i = 0 ; i < cookiesArr.length;i++) { var attArr = cookiesArr[i].split("="); if(attArr[0] == "username") { uer.value = attArr[1]; } if(attArr[0] == "password") { pwd.value= attArr[1]; } } </script>