本文为大家分享了js和jQuery记住密码功能的方法,具体如下
一. js 实现记住密码功能
html:
<form id="form22" name="form22" action="checklogin.action" method="post" > <div class="username" style="margin-top:50px;"> <label>用户名:</label> <input type="text" name="username" id="userName" /> <span id="myuser" style="color: red; font-size:12px; font-weight: normal;"></span> </div> <div class="password"> <label>密 码:</label> <input name="password" id="passWord" type="password" /> <span id="mypass" style="color: red; font-size:12px; font-weight: normal;"></span> </div> <div class="user_type" > <label> </label> <input type="checkbox" id="saveUserName" style="float: left; margin-top:3px;" /> <span> 记住用户</span> </div> <input type="button" value="" class="btn_login" id="btn_login" onclick="checkform();"/></form>
cookie.js:
function setCookie (name, value) { var Days = 30; //此 cookie 将被保存 30 天 var exp = new Date(); exp.setTime(exp.getTime() + 1000); if(value==""||value=="null" ||value=="null"||value==" "){ }else{ document.cookie = name + "="+ escape(value) +";expires=Sun, 17-Jan-2038 19:14:07 GMT"; }}function getCookie(sName){ var aCookie = document.cookie.split("; "); for (var i=0; i < aCookie.length; i++) { var aCrumb = aCookie[i].split("="); if (sName == aCrumb[0]) { return aCrumb[1]; } } return null;}function checkCookieExist(name){ if (getCookie(name)) return true; else return false;}function deleteCookie(name, path, domain){ var strCookie; // 检查Cookie是否存在 if (checkCookieExist(name)){ // 设置Cookie的期限为己过期 strCookie = name + "="; strCookie += (path) ? "; path=" + path : ""; strCookie += (domain) ? "; domain=" + domain : ""; strCookie += "; expires=Thu, 01-Jan-70 00:00:01 GMT"; document.cookie = strCookie; }}function saveCookie(name, value, expires, path, domain, secure){ var strCookie = name + "=" + value; if (expires){ // 计算Cookie的期限, 参数为天数 var curTime = new Date(); curTime.setTime(curTime.getTime() + expires*24*60*60*1000); strCookie += "; expires=" + curTime.toGMTString(); } // Cookie的路径 strCookie += (path) ? "; path=" + path : ""; // Cookie的Domain strCookie += (domain) ? "; domain=" + domain : ""; // 是否需要保密传送,为一个布尔值 strCookie += (secure) ? "; secure" : ""; document.cookie = strCookie;}
login.js
window.onload = function(){ //console.log("ctx: " + "${ctx}"); var name = getCookie("loginUserName"); document.getElementById("passWord").value=""; if(name != null && name != "") { document.getElementById("userName").value = name; document.getElementById("passWord").focus(); } else { document.getElementById("userName").focus(); }}function checkform(){ ...... var isChecked = document.getElementById("saveUserName").checked; if(isChecked) { setCookie("loginUserName",userName); } ......}
新闻热点
疑难解答
图片精选