我一直在研究这个代码用于我的GCSE评估,但我似乎无法使JavaScript工作,我已经查看了我的代码几个星期了,我仍然无法找到内部的逻辑错误我怀疑我错过了一些让Java工作的东西,如果有人可以帮助它会非常有帮助
我正在浏览的当前代码:
<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.getElemenById('name').style.color="red";
result = false;
}
if (document.ExamEntry.subject.value=="") {
msg+="You must enter the subject \n";
document.ExamEntry.subject.focus();
document.geElementById('subject').style.color="red";
result = false;
}
if (document.ExamEntry.exam number.value=="") {
msg+="You must enter your exam number \n";
document.ExamEntry.subject.focus();
document.geElementById('exam number').style.color="red";
result = 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="exam number">Exam Number</td>
<td><input type="text" name="Exam Number" /></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>
</table>
</form>
</body>
答案 0 :(得分:1)
整理你的代码片段提出了真正的问题:
function ValidateForm() {
var result = true;
var msg="";
if (document.ExamEntry.name.value=="") {
msg+="You must enter your name \n";
document.ExamEntry.name.focus();
document.getElemenById('name').style.color="red";
result = false;
}
if (document.ExamEntry.subject.value=="") {
msg+="You must enter the subject \n";
document.ExamEntry.subject.focus();
document.geElementById('subject').style.color="red";
result = false;
}
if (document.ExamEntry.exam number.value=="") {
msg+="You must enter your exam number \n";
document.ExamEntry.subject.focus();
document.geElementById('exam number').style.color="red";
result = false;
}
if(msg==""){
return result;
}
{
alert(msg)
return result;
}
&#13;
<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="exam number">Exam Number</td>
<td><input type="text" name="Exam Number" /></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>
</table>
&#13;
SyntaxError: missing ) after condition
if (document.ExamEntry.exam number.value=="") {
---------------------------^
如果您的变量中有空格,则无法使用点表示法,但可以使用方括号表示法。将该行更改为:
if (document.ExamEntry["Exam Number"].value=="") {