您好,登錄后才能下訂單哦!
本篇內(nèi)容主要講解“js如何實(shí)現(xiàn)登錄時(shí)記住密碼”,感興趣的朋友不妨來看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“js如何實(shí)現(xiàn)登錄時(shí)記住密碼”吧!
JS是JavaScript的簡(jiǎn)稱,它是一種直譯式的腳本語言,其解釋器被稱為JavaScript引擎,是瀏覽器的一部分,主要用于web的開發(fā),可以給網(wǎng)站添加各種各樣的動(dòng)態(tài)效果,讓網(wǎng)頁更加美觀。
常見的很多網(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è)置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 > <span><input type="checkbox" id="remember"><label for="remember">記住我</label></span> <span >注冊(cè)</span> </div> <button type="button" class="inp" id="btn">立即登錄</button> </form>
注意js里邊的id對(duì)應(yīng):
到此,相信大家對(duì)“js如何實(shí)現(xiàn)登錄時(shí)記住密碼”有了更深的了解,不妨來實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。