html 表单 dom 注意跟表单的name值一致
<script type="text/javascript"> function checkForm() { var theForm = document.form1; if(theForm.username.value=="") //html 表单 dom 注意跟表单的name值一致 { window.alert("用户名不能为空"); return false; }else if(theForm.userpwd.value.length==0) { window.alert("密码不能为空"); return false; }else { window.alert("验证通过"); return true; } } </script> </head> <body> <form name="form1" method="get" action="login.php" onsubmit="return checkForm()"> 用户名:<input type="text" name="username" /> 密码:<input type="password" name="userpwd" /> <input type="submit" value="提交表单" /> </form>