登录之回车登录和记住密码
页面:
<div> <dl> <dd><input name="txtUser" type="text" id="txtUser" placeholder="用户名/邮箱/手机号" /></dd> </dl>
<dl> <dd><input type="password" id="Userpwd" onKeyDown="KeyDown()" placeholder="请输入您的密码" /></dd> </dl>
<div> <label><input id="remember" type="checkbox"><span class="ml10" style="color:#999">记住账号和密码</span></label> </div>
<button onClick="cliLogin()" id="logbtn" style="outline:none">登 录</button> </div>
js方法:
//回车登录
function KeyDown(){
if (event.keyCode==13){ //回车键的键值为13
cliLogin(); ////调用登录按钮的登录事件
}
}
//登录操作
function cliLogin() {
var txtUser = $.trim($("#txtUser").val());
var txtPwd = $("#Userpwd").val();
if ($.trim(txtUser) == "") {
alert('请输入账号!');
return;
}
if ($.trim(txtPwd) == "") {
alert('请输入密码!');
return;
}
//账号和密码都有时根据后台返回的登录状态success或者failure做判断,当是success时添加以下代码
if(remember.checked){//记住密码
setCookie('zhiguiuser',txtUser,7); //保存帐号到cookie,有效期7天
setCookie('zhiguipassword',txtPwd,7); //保存密码到cookie,有效期7天
}else{//取消记住密码
delCookie('zhiguiuser');
delCookie('zhiguipassword');
}
}
//页面加载时判断时候有cookie缓存
window.onload = function(){
//分析cookie值,显示上次的登陆信息
var oRemember = document.getElementById("remember");
if(getCookie("zhiguiuser") && getCookie("zhiguipassword")){
$("#txtUser").val(getCookie("zhiguiuser"));
$("#Userpwd").val(getCookie("zhiguipassword"));
oRemember.checked = true;
}
};
//设置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);
};
识不足则心多虑,威不足则情多怒,信不足则口多言,当养谦卑之气