js 回车键触发登录,并且验证输入内容
源码:
<!DOCTYPE html>
<html lang="en">
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<link rel="stylesheet" href="css/text.css" type="text/css">
<script language="javascript">
function check() {
var formname=document.loginForm;
if (formname.userName.value == "") {
alert("请输入用户名!");
formname.userName.focus();
return false;
}
if (formname.pwd.value == "") {
alert("请输入密码!");
formname.pwd.focus();
return false;
}
formname.submit();
}
//回车时,默认是登陆
function on_return(){
if(window.event.keyCode == 13){
if (document.all('sub')!=null){
document.all('sub').click();
}
}
}
</script>
</head>
<body onkeydown="on_return();">
<form name ="loginForm" method="post" action="#">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="69%" height="30"><span class="font_04">帐户名</span>
<input type="text" name="userName">
</td>
</tr>
<tr>
<td width="69%" height="30"><span class="font_04">密 码</span>
<input type="password" name="pwd" >
</td>
</tr>
<tr>
<td width="31%" height="30">
<a id="sub" onClick='check()' style="color: brown">
登陆</a></td>
</tr>
</table>
</form>
</body>
</html>