0

每次我在未填写正确信息时单击提交时,每个部分都会有一条警告消息,并且文本应该变成红色,但它正在链接到链接的文件。如果有人可以帮助解决这个问题,我会非常感激。

<html>
<head>
<title>Exam Entry</title>
<script language="javascript" type="text/javascript"> 
function validateForm() {
var result = true;
var msg="";
if (document.ExamEntry.Name.value=="") {
msg+="You must enter your Name \n";
document.ExamEntry.Name.focus();
document.getElementById('Name').style.color="red";
result = false;
}
if (document.ExamEntry.Subject.value=="") {
msg+="You must enter the Subject \n";
document.ExanEntry.Subject.focus();
document.getElementById('Subject').style.color="red";
result = false;
}
if (document.ExamEntry.ExamNumber.value=="") {
msg+="You must enter the ExamNumber \n";
document.ExanEntry.ExamNumber.focus();
document.getElementById('ExamNumber').style.color="red";
result = false;
}
if (document.ExamEntry.ExamNumber.value.length!=4) {
msg+="You must enter at least Four Numbers in the ExamNumber \n";
document.ExamEntry.ExamNumber.focus();
document.getElementById('ExamNumber').style.color="red";
result = false;
} 
if ( ( Examentry.Exam Level[0].checked == false ) && ( Examentry.Exam Level[1].checked == false )  && ( Examentry.Exam Level[2].checked == false )) 
{
alert 
 ( "Please select you Exam Level" ); 
  return false;
}
if(msg==""){
return result;
}
{
alert(msg)
return result;
}
}
</script>
</head>
<body>
<h1>Exam Entry Form</h1>
<form name="ExamEntry" method="post" action="success.html">
<table width="50%" border="0">
<tr>
<td id="Name">Name</td>
<td><input type="text" name="Name" /></td>
</tr>
<tr>
<td id="Subject">Subject</td>
<td><input type="text" name="Subject" /></td>
</tr>
<tr>
<td id="ExamNumber">ExamNumber</td>
<td><input type="text" name="ExamNumber" Length="4"/>
<front size="1">(Maximum characters: 4)</font></td>
</tr>
<tr>
<td id="Exam Level">Exam Level</td>
<td><input type="radio" name="age" value="GCSE">GCSE<br>
<input type="radio" name="Exam Level" value="AS">AS<br>
<input type="radio" name="Exam Level" value="A2">A2</td>
</tr>
<tr>
<td><input type="submit" name="Submit" value="Submit" onclick="return validateForm();" /></td>
<td><input type="reset" name="Reset" value="Reset" /></td>
</tr>
<tr>
</tr>
</table>
</form>
</body>
</html>

这是它链接到的文件...

<head>
<title>Success message</title>
</head>
<body>
<H1> You entered all the data required </H1>
</body>
4

2 回答 2

1

这一行:

if ( ( Examentry.Exam Level[0].checked == false ) && ( Examentry.Exam Level[1].checked == false )  && ( Examentry.Exam Level[2].checked == false )) 

包含阻止脚本运行的语法错误(“Exam”和“Level”之间的空格)。如果删除 "Exam" 和 "Level" 之间的空格,它将起作用:

 if ( ( Examentry.ExamLevel[0].checked == false ) && ( Examentry.ExamLevel[1].checked == false )  && ( Examentry.ExamLevel[2].checked == false )) 

更新:您还需要修改实际 HTML 元素本身的 id 标记以删除空格。例如<td id="Exam Level">变成<td id="ExamLevel">

我建议保留您的浏览器开发人员。工具(大多数浏览器中的 F12)在测试这样的东西时打开,因为脚本语法错误通常会立即被标记。

于 2013-09-17T16:01:37.427 回答
0

这可能是你的问题。消除;

function validateForm(); { //-----> Remove ;
//rest of your code
}

你也应该看看validationEngineplugin 或jQuery validate.

于 2013-09-17T12:07:49.227 回答