-1

我已经准备好插入单选按钮的验证,现在我需要的是在代码继续询问“您确定要选择'x'”之前出现一个警报框,x 将是选定的级别。如果按下取消,它将返回网页。谢谢你在这里是我下面的代码。

<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.number.value == "") {
        msg += "You must enter a exam number \n";
        document.ExamEntry.number.focus();
        document.getElementById('number').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.level[0].checked == false) && (document.ExamEntry.level[1].checked == false) && (document.ExamEntry.level[2].checked == false)) {
        msg += "You mus select the level entered \n";
        result = false;
    }
}
function CheckField(obj) {
    if (obj.value.length < '4') {
        alert('Name field must 4 character long');
        obj.focus();
    }
}


</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="number">Examination Number</td>
<td><input type="text" name="number" maxlength="4" onblur="CheckField(this)"/></td>
</tr>
<tr>
<td id="subject">Subject</td>
<td><input type="text" name="subject" /></td>
</tr>
<tr>
<input type="radio" name="level">GCSE
<input type="radio" name="level">AS
<input type="radio" name="level">A2
<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>
</html>
4

1 回答 1

0

希望这有助于相处。

<!DOCTYPE html>
<html>
<head>
    <title>Exam entry</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <script type="text/javascript">
        function validateForm() {
            var result = true;
            var msg = "";
            var objx;

            // test for empty inputs

            objx = document.getElementById("subjectInput");
            if (objx.value.length == 0) {
                msg += "You must enter the subject \n";
                objx.focus();
                document.getElementById('subject').style.color = "red";
                result = false;
            }

            objx = document.getElementById("numberInput");
            if (objx.value.length == 0) {
                msg += "You must enter a exam number \n";
                objx.focus();
                document.getElementById('number').style.color = "red";
                result = false;
            }

            objx = document.getElementById("nameInput");
            if (objx.value.length == 0) {
                msg += "You must enter your name \n";
                objx.focus();
                document.getElementById('name').style.color = "red";
                result = false;
            }


            // checked level is needed below for confirmation
            var objx = document.ExamEntry;
            var checkedLevel = "";
            if (objx.level[0].checked) {
                checkedLevel = objx.level[0].value;
            }
            else
            if (objx.level[1].checked) {
                checkedLevel = objx.level[1].value;
            }
            else
            if (objx.level[2].checked) {
                checkedLevel = objx.level[2].value;
            }

            if (checkedLevel.length == 0) {
                msg += "You must select the level entered \n";
                result = false;
            }

            if (result) {
                // test for minimum length of name
                objx = document.getElementById("nameInput");
                if (objx.value.length < 4)
                {
                    objx.focus();
                    document.getElementById('name').style.color = "red";
                    //alert('Name field must 4 character long');
                    msg += 'Name field must be 4 characters long'
                    result = false;
                }
            }

            // confirm checked level
            if (result) {
                result = confirm("Are you sure you want to pick '" + checkedLevel + "' ?");
            }
            else {
                alert(msg);
            }
            return result;
        }
    </script>
</head>
<body>
    <h1>Exam Entry Form</h1>
    <form name="ExamEntry" method="post" onsubmit="return validateForm();" action="success.html">
        <table width="50%" border="0">
            <tr>
                <td id="name">Name</td>
                <td><input type="text" name="name" id="nameInput" /></td>
            </tr>
            <tr>
                <td id="number">Examination Number</td>
                <td><input type="text" name="number" id="numberInput" maxlength="4" /></td>
            </tr>
            <tr>
                <td id="subject">Subject</td>
                <td><input type="text" name="subject" id="subjectInput" /></td>
            </tr>
            <tr>
            <input type="radio" name="level" value="GCSE">GCSE
            <input type="radio" name="level" value="AS">AS
            <input type="radio" name="level" value="A2">A2
            <tr>
                <td><input type="submit" name="Submit" value="Submit" />         </td>
                <td><input type="reset" name="Reset" value="Reset" /></td>
            </tr>
        </table>
    </form>
</body>
</html>
于 2013-10-13T11:54:33.880 回答