Javascript警报没有被解雇

时间:2013-10-18 12:43:31

标签: javascript

由于某些原因,我的java脚本正在改变文本的颜色,但没有显示我的错误信息,任何帮助都会很棒!

代码是一个简单的HTML表单,要求审查员姓名,号码,主题,还有一些单选按钮

<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.ExamEntry.subject.focus();
        document.getElementById('subject').style.color="red";
        result = false;
    }

    if (document.ExamEntry.ExaminationNumber.value=="") {
        msg+="You must enter the Examination Number \n";
        document.ExamEntry.ExaminationNumber.focus();
        document.getElementById('Examination 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="S:\codes\success.html">
<table width="50%" border="0">
<tr>
        <td id="name">Name</td>
        <td><input type="text" name="name" /></td>
        <title>Exam entry</title>
    <tr>
        <td id="subject">Subject</td>
        <td><input type="text" name="subject" /></td>
    </tr>
    <tr>
        <td id="Examination Number">ExaminationNumber</td>
        <td><input type="text" name="Examination Number" /></td>
    </tr>
 <tr>
        <td><input type="submit" name="Submit" value="Submit" onclick="return validateForm();" /></td>
<tr>
        <td><input type="reset" name="Reset" value="Reset" /></td>
    <input type="radio" name="qualification" value="GCSE">GCSE<br>
    <input type="radio" name="qualification" value="AS">AS<br>
    <input type="radio" name="qualification" value="A2">A2<br>
</form>
</tr>
</table>
</body>
</HTML>

1 个答案:

答案 0 :(得分:2)

ID不能包含空格..将Examination Number更改为Examination_NumberExaminationNumber

修正 jsFiddle example