欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

js實(shí)現(xiàn)登錄時記住密碼的方法分析

 更新時間:2020年04月05日 11:31:40   作者:一枕江風(fēng)  
這篇文章主要介紹了js實(shí)現(xiàn)登錄時記住密碼的方法,結(jié)合實(shí)例形式分析了JavaScript基于cookie實(shí)現(xiàn)存儲登錄密碼相關(guā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è)置cookie
function setCookie(name, value, day) {
 var date = new Date();
 date.setDate(date.getDate() + day);
 document.cookie = name + '=' + value + ';expires=' + date;
};
//獲取cookie
function getCookie(name) {
 var reg = RegExp(name + '=([^;]+)');
 var arr = document.cookie.match(reg);
 if (arr) {
 return arr[1];
 } else {
 return '';
 }
};
//刪除cookie
function delCookie(name) {
 setCookie(name, null, -1);
};

登錄頁面

<form id="myForm" action="login" method="post">
 <input type="text" value="" class="inp" name = "username" id="username" />
 <input type="password" value="" class="inp" name = "password" id="passwrod" />
 
 <input type="text" class="inp" id="yzm" placeholder="驗(yàn)證碼" />
 <img id="img" src="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" class="inp" 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è)計(jì)有所幫助。

相關(guān)文章

最新評論