js實(shí)現(xiàn)登錄時(shí)記住密碼的方法分析
本文實(shí)例講述了js實(shí)現(xiàn)登錄時(shí)記住密碼的方法。分享給大家供大家參考,具體如下:
常見的很多網(wǎng)站登錄,都有記住密碼功能,下面是用js實(shí)現(xiàn)的記住密碼功能(代碼用的源生js,不用引入任何插件,大家如果引入了jQuery,可以進(jìn)行修改,優(yōu)化)
js部分
window.onload = function(){ var oForm = document.getElementById(’myForm’); var oUser = document.getElementById(’username’); var oPswd = document.getElementById(’passwrod’); var oRemember = document.getElementById(’remember’); //頁面初始化時(shí),如果帳號(hào)密碼cookie存在則填充 if (getCookie(’username’) && getCookie(’password’)) { oUser.value = getCookie(’username’); oPswd.value = getCookie(’password’); oRemember.checked = true; } //復(fù)選框勾選狀態(tài)發(fā)生改變時(shí),如果未勾選則清除cookie oRemember.onchange = function() { if (!this.checked) { delCookie(’username’); delCookie(’password’); } }; //表單提交事件觸發(fā)時(shí),如果復(fù)選框是勾選狀態(tài)則保存cookie oForm.onsubmit = function() { if (remember.checked) { setCookie(’username’, oUser.value, 7); //保存帳號(hào)到cookie,有效期7天 setCookie(’password’, oPswd.value, 7); //保存密碼到cookie,有效期7天 } };};//設(shè)置cookiefunction setCookie(name, value, day) { var date = new Date(); date.setDate(date.getDate() + day); document.cookie = name + ’=’ + value + ’;expires=’ + date;};//獲取cookiefunction getCookie(name) { var reg = RegExp(name + ’=([^;]+)’); var arr = document.cookie.match(reg); if (arr) { return arr[1]; } else { return ’’; }};//刪除cookiefunction delCookie(name) { setCookie(name, null, -1);};
登錄頁面
<form action='login' method='post'> <input type='text' value='' name = 'username' /> <input type='password' value='' name = 'password' /> <input type='text' placeholder='驗(yàn)證碼' /> <img src='http://www.aoyou183.cn/bcjs/getCode' onclick='changeImg()'> <div style='margin: 10px;'> <span><input type='checkbox' id='remember'><label for='remember'>記住我</label></span> <span style='float: right;'>注冊(cè)</span> </div> <button type='button' id='btn'>立即登錄</button></form>
注意js里邊的id對(duì)應(yīng):
更多關(guān)于JavaScript相關(guān)內(nèi)容可查看本站專題:《JavaScript操作DOM技巧總結(jié)》、《JavaScript頁面元素操作技巧總結(jié)》、《JavaScript事件相關(guān)操作與技巧大全》、《JavaScript查找算法技巧總結(jié)》、《JavaScript數(shù)據(jù)結(jié)構(gòu)與算法技巧總結(jié)》、《JavaScript遍歷算法與技巧總結(jié)》及《JavaScript錯(cuò)誤與調(diào)試技巧總結(jié)》
希望本文所述對(duì)大家JavaScript程序設(shè)計(jì)有所幫助。
相關(guān)文章:
1. ajax請(qǐng)求添加自定義header參數(shù)代碼2. ASP基礎(chǔ)知識(shí)VBScript基本元素講解3. Gitlab CI-CD自動(dòng)化部署SpringBoot項(xiàng)目的方法步驟4. Kotlin + Flow 實(shí)現(xiàn)Android 應(yīng)用初始化任務(wù)啟動(dòng)庫5. Python requests庫參數(shù)提交的注意事項(xiàng)總結(jié)6. 淺談SpringMVC jsp前臺(tái)獲取參數(shù)的方式 EL表達(dá)式7. 利用CSS3新特性創(chuàng)建透明邊框三角8. asp知識(shí)整理筆記4(問答模式)9. ASP中解決“對(duì)象關(guān)閉時(shí),不允許操作。”的詭異問題……10. 詳談ajax返回?cái)?shù)據(jù)成功 卻進(jìn)入error的方法
