第1个回答 2015-01-14
<script language="javascript" type="text/javascript">
function checkInCorrect() //判断用户名和密码是否为空
{
if (document.getElementById('txtUserName').value=="")
{
alert('请输入用户名!')
document.getElementById('txtUserName').focus();
return false
}
if (document.getElementById('txtPassword').value=="")
{
alert('请输入密码!')
document.getElementById('txtPassword').focus();
return false
}
else
{
saveInfo();
return true;
}
}
saveInfo = function(){
try{
var isSave = document.getElementById('chkRememberPwd').checked; //保存按键是否选中
if (isSave) {
var usernm = document.getElementById('txtUserName').value;
var userpsw = document.getElementById('txtPassword').value;
if(usernm!="" && userpsw!=""){
SetCookie(usernm,userpsw);
}
}else {
SetCookie("","");
}
}catch(e){
}
}
function SetCookie(usern,psw){
var Then = new Date()
Then.setTime(Then.getTime() + 1866240000000)
document.cookie ="username=" + usern + "%%"+psw+";expires="+ Then.toGMTString() ;
}
function GetCookie(){
var nmpsd;
var nm;
var psd;
var cookieString = new String(document.cookie)
var cookieHeader = "username="
var beginPosition = cookieString.indexOf(cookieHeader)
cookieString = cookieString.substring(beginPosition);
var ends=cookieString.indexOf(";");
if (ends!=-1){
cookieString = cookieString.substring(0,ends);
}
if (beginPosition>-1){
nmpsd = cookieString.substring(cookieHeader.length);
if (nmpsd!=""){
beginPosition = nmpsd.indexOf("%%");
nm=nmpsd.substring(0,beginPosition);
psd=nmpsd.substring(beginPosition+2);
document.getElementById('txtUserName').value=nm;
document.getElementById('txtPassword').value=psd;
if(nm!="" && psd!=""){
document.forms[0].checkbox.checked = true
}
}
}
}
</script>
</head>
<body onLoad="document.getElementById('txtUserName').focus();GetCookie();">
<form>
用户名:<input type="text" ID="txtUserName" onblur="GetPwdAndChk()"> <br>
密 码:<input type="password" ID="txtPassword"> <br>
<input type="checkbox" ID="chkRememberPwd" />记住密码 <br>
<input type="button" OnClick="checkInCorrect()" value="进入"/>
</form>
</body>