1

我是 javascript 新手,我正在在线家庭教程中自学。我的 Javacript 教程要求我尝试基本的表单代码。

但是函数 checkRadio()没有发布错误消息!(如果单选按钮未选中 - 然后发布错误消息)

函数 checkEmail()确实正确地发布了错误消息。

我已经检查了很多次语法。你能发现错误吗?

谢谢

<!DOCTYPE HTML>
<HTML>
    <HEAD>
        <TITLE>Javascript and Forms</TITLE>

        <SCRIPT LANGUAGE = "Javascript">

        function validate() {

            var emailError = checkEmail();
            var radioError = checkRadio();
            var dropdownError = checkDropdown();
            var checkboxError = checkCheckbox();

        }




        function checkEmail(){

            var email = document.frmOne.email.value;
            if(email==""){
                document.getElementById("email_error").innerHTML = "No Email Adress";
                return false;
            }
            else{
                document.getElementById("email_error").innerHTML = "";
                return true;
            }




        }

        function checkRadio(){

            var payment = "";
            var len = document.frmone.payment.length;
            var i;

            for(i = 0;i<len;i++){
                if (document.frmone.payment[i].checked){
                    payment = document.frmone.payment[i].value;
                    break;
                }
            }
            if(payment == ""){
                document.getElementById("radio_error").innerHTML = "No payment Option Selected";
                return false;
            }
            else{
                document.getElementById("radio_error").innerHTML = "";
                return true;
            }
        }







        </SCRIPT>
    </HEAD>

    <BODY>

        <FORM NAME="frmOne" ACTION="" METHOD="POST">

        <P>
            EMAIL: <INPUT TYPE="text" NAME="email" VALUE="" PLACEHOLDER="Email Address"> 
            <SPAN STYLE=color:red ID="email_error"></SPAN>
        </P>

        <P>
            Payment Options:
            <INPUT TYPE="Radio" Name="payment" Value="CC">Credit Card
            <INPUT TYPE="Radio" Name="payment" Value="DC">Debit Card
            <INPUT TYPE="Radio" Name="payment" Value="PP">PayPal 
            <SPAN STYLE=color:red ID="radio_error"></SPAN>
        </P>

        <P>
        Where did you hear about us?
        <SELECT NAME="hear">
            <OPTION Value="None">None</OPTION>
            <OPTION Value="paper">Newspaper</OPTION>
            <OPTION Value="tv">Television</OPTION>
            <OPTION Value="mouth">Word of Mouth</OPTION>
            <OPTION Value="seach">Search Engine</OPTION>
        </SELECT>
         <SPAN STYLE=color:red ID="dropdown_error"></SPAN>
        </P>

        <P>
            <LABEL FOR="CB1">Terms and Conditions</LABEL>
            <INPUT TYPE="Checkbox" Name="terms" ID="CB1" Value="TAC">
            <SPAN STYLE=color:red ID="checkbox_error"></SPAN>
        </P>

        <P>&nbsp;</P>
        <INPUT TYPE="button" VALUE="  SUBMIT  " onClick="validate()">
        </FORM>

    </BODY>
</HTML>
4

2 回答 2

1

你错过了这一行的一个大写字母:

len = document.frmone.payment.length;

应该len = document.frmOne.payment.length;

您应该学习使用浏览器的网络检查器,它可以帮助您轻松发现这些东西。

于 2013-01-18T14:20:03.750 回答
1

有3个地方需要frmonefrmOne。一个是马可指出的。其他地方是

if (document.frmOne.payment[i].checked){
                    payment = document.frmOne.payment[i].value;
                    break;
                }
于 2013-01-18T14:37:05.620 回答