JS代码无法正常运行,请教……
本帖最后由 hawzt1 于 2020-6-5 12:10 编辑<!DOCTYPE html>
<html>
<head>
<title>表单注册和验证</title>
<script>
function check(){
fr=document.form1;
if(fr.uid.value == ""){
alret("用户ID必须要写!");
fr.uid.focus();
return false;
}
if((fr.pew1.value != "") || (fr.psw.value != "")){
if(fr.psw1.value != fr.psw2.value){
alert("密码不一致,请重新输入并验证密码!");
fr.psw1.focus();
return false;
}
}else{
alert("密码不能为空!");
fr.psw1.focus();
return false;
}
if(fr.gender.value=="")
{
alert("性别必须要填写!");
fr.name.focus();
return false;
}
fr.submit();
}
</script>
</head>
<body>
<form name="form1" id="form1" method="get">
<table border="1" align="center" width=350>
<tr align="middle">
<th colSpan=2 height=24>新用户注册</th>
</tr>
<tr>
<td width="40%">
<b>用 户 名</b>:
</td>
<td width="60%">
<input maxLength="10" size=30 name="uid" type="text">
</td>
</tr>
<tr>
<td>
<b>性 别</b>:
</td>
<td>
<input type=radio CHECKED value="boy" name="gender">男孩
<input type=radio value="girl" name="gender">女孩
</td>
</tr>
<tr>
<td>
<b>密 码</b>:
</td>
<td>
<input name="psw1" type="password" size=32>
</td>
</tr>
<tr>
<td>
<b>确认密码</b>:
</td>
<td>
<input name="psw2" type="password" size=32>
</td>
</tr>
<tr>
<td>
<b>密码问题</b>:
</td>
<td>
<input type=text size=30 name="question" type="text">
</td>
</tr>
<tr>
<td>
<b>问题答案</b>:
</td>
<td>
<input type=text size=30 name="answer" type="text">
</td>
</tr>
<tr>
<td>
<b>Email</b>:
</td>
<td>
<input maxlength=50 size=30 name="aemail" type="text">
</td>
</tr>
<tr>
<td>
<b>联系电话</b>:
</td>
<td>
<input maxlength=50 size=30 name="answer" type="text">
</td>
</tr>
<tr>
<td>
<b>职 业</b>:
</td>
<td>
<select name="career" class="input1">
<option value="student" selected="selected">学生</option>
<option value="worker">工人</option>
<option value="teacher">老师</option>
<option value="famer">农民</option>
<option value="business">商人</option>
</td>
</tr>
<tr>
<td>
</td>
<td>
<input type=submit value="注 册" name=Submit onclick="return check()">
<input type="reset" value="清 除" name=Submit2>
</td>
</tr>
</table>
</form>
</body>
</html> 挪到128行之后 <!DOCTYPE html>
<html>
<head>
<title>表单注册和验证</title>
</head>
<body>
<form name="form1" id="form1" method="get">
<table border="1" align="center" width=350>
<tr align="middle">
<th colSpan=2 height=24>新用户注册</th>
</tr>
<tr>
<td width="40%">
<b>用 户 名</b>:
</td>
<td width="60%">
<input maxLength="10" size=30 name="uid" type="text">
</td>
</tr>
<tr>
<td>
<b>性 别</b>:
</td>
<td>
<input type=radio CHECKED value="boy" name="gender">男孩
<input type=radio value="girl" name="gender">女孩
</td>
</tr>
<tr>
<td>
<b>密 码</b>:
</td>
<td>
<input name="psw1" type="password" size=32>
</td>
</tr>
<tr>
<td>
<b>确认密码</b>:
</td>
<td>
<input name="psw2" type="password" size=32>
</td>
</tr>
<tr>
<td>
<b>密码问题</b>:
</td>
<td>
<input type=text size=30 name="question" type="text">
</td>
</tr>
<tr>
<td>
<b>问题答案</b>:
</td>
<td>
<input type=text size=30 name="answer" type="text">
</td>
</tr>
<tr>
<td>
<b>Email</b>:
</td>
<td>
<input maxlength=50 size=30 name="aemail" type="text">
</td>
</tr>
<tr>
<td>
<b>联系电话</b>:
</td>
<td>
<input maxlength=50 size=30 name="answer" type="text">
</td>
</tr>
<tr>
<td>
<b>职 业</b>:
</td>
<td>
<select name="career" class="input1">
<option value="student" selected="selected">学生</option>
<option value="worker">工人</option>
<option value="teacher">老师</option>
<option value="famer">农民</option>
<option value="business">商人</option>
</td>
</tr>
<tr>
<td>
</td>
<td>
<input type=submit value="注 册" name=Submit onclick="return check()">
<input type="reset" value="清 除" name=Submit2>
</td>
</tr>
</table>
</form>
<script>
function check(){
fr=document.form1;
if(fr.uid.value == ""){
alret("用户ID必须要写!");
fr.uid.focus();
return false;
}
if((fr.pew1.value != "") || (fr.psw.value != "")){
if(fr.psw1.value != fr.psw2.value){
alert("密码不一致,请重新输入并验证密码!");
fr.psw1.focus();
return false;
}
}else{
alert("密码不能为空!");
fr.psw1.focus();
return false;
}
if(fr.gender.value=="")
{
alert("性别必须要填写!");
fr.name.focus();
return false;
}
fr.submit();
}
</script>
</body>
</html> html文件是从前往后加载的,加载到script标签直接就会运行。你的代码写在要获取的东西前面,执行script时就会获取不到。可以用window.onload(窗口加载完成时的事件)或者把script标签挪到body最后面
页:
[1]