未捕获引用错误:未定义validateform

Uncaught Reference Error: validateform is not definded

本文关键字:未定义 validateform 错误 引用      更新时间:2023-09-26

代码非常简单。但这是我做的第一件事,我根本不明白我做错了什么。这也是直接来自OCR的原始代码。

这是普通中等教育证书计算机课程。我不必修复它,但我不知道在基本代码不起作用的情况下,我应该如何测试我要添加的东西。

<head>
    <title>Exam entry</title>
    <script language="javascript" type="text/javascript">
        function validateForm(document) {
            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 (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>
                    <input type="submit" name="Submit" value="Submit" onclick= return "validateForm()";  />
                </td>
                <td>
                    <input type="reset" name="Reset" value="Reset" />
                </td>
            </tr>
        </table>
    </form>
</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>
                    <input type="submit" name="Submit" value="Submit" onclick= " return validateForm();"  /><!--js function inside quotes-->
                </td>
                <td>
                    <input type="reset" name="Reset" value="Reset" />
                </td>
            </tr>
        </table>
    </form>
<script>
//function had illegal parameter
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";
                 // document.getElementById(‘name’).style.color = "red";--->Id should be in quotes
                result = false;
            }
            if (document.ExamEntry.subject.value == "") {
                msg += "You must enter the subject 'n";
                document.ExamEntry.subject.focus();
                document.getElementById('subject').style.color = "red";
                // document.getElementById(‘subject’).style.color = "red";--->Id should be in quotes
                result = false;
            }
            if (msg == "") {
                return result;
            } {
                alert(msg);
                return result;
            }
        }
</script>