js實(shí)現(xiàn)登錄時記住密碼的方法分析
本文實(shí)例講述了js實(shí)現(xiàn)登錄時記住密碼的方法。分享給大家供大家參考,具體如下:
常見的很多網(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’); //頁面初始化時,如果帳號密碼cookie存在則填充 if (getCookie(’username’) && getCookie(’password’)) { oUser.value = getCookie(’username’); oPswd.value = getCookie(’password’); oRemember.checked = true; } //復(fù)選框勾選狀態(tài)發(fā)生改變時,如果未勾選則清除cookie oRemember.onchange = function() { if (!this.checked) { delCookie(’username’); delCookie(’password’); } }; //表單提交事件觸發(fā)時,如果復(fù)選框是勾選狀態(tài)則保存cookie oForm.onsubmit = function() { if (remember.checked) { setCookie(’username’, oUser.value, 7); //保存帳號到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.lshqa.cn/bcjs/getCode' onclick='changeImg()'> <div style='margin: 10px;'> <span><input type='checkbox' id='remember'><label for='remember'>記住我</label></span> <span style='float: right;'>注冊</span> </div> <button type='button' id='btn'>立即登錄</button></form>
注意js里邊的id對應(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錯誤與調(diào)試技巧總結(jié)》
希望本文所述對大家JavaScript程序設(shè)計有所幫助。
相關(guān)文章:
1. Java發(fā)送http請求的示例(get與post方法請求)2. 基于android studio的layout的xml文件的創(chuàng)建方式3. Java之JSP教程九大內(nèi)置對象詳解(中篇)4. 詳解CSS不定寬溢出文本適配滾動5. CSS自定義滾動條樣式案例詳解6. 基于python計算滾動方差(標(biāo)準(zhǔn)差)talib和pd.rolling函數(shù)差異詳解7. Python如何向SQLServer存儲二進(jìn)制圖片8. 詳解Python中openpyxl模塊基本用法9. python numpy實(shí)現(xiàn)rolling滾動案例10. python opencv 實(shí)現(xiàn)讀取、顯示、寫入圖像的方法
