onSubmit函数无法调用其中的函数

时间:2014-12-12 01:43:56

标签: javascript html function onsubmit

我正在尝试制作表格。我希望它检查单选按钮以查看它们是否已被单击,如果没有要向用户发送消息以检查单选按钮。

我试着输入它,然后我尝试继续我的其他if语句(得到错误消息),然后我尝试在onsubmit函数中创建一个函数(它根本没有启动),然后我尝试制作onsubmit函数之外的函数,我试图调用它,但它不会启动。我甚至试过在onsubmit函数的顶部或下面移动函数。

我做了submitYesCancel以查看问题是否与radioB函数有关,但两个函数都不会启动。

我绝望地被困住了。请帮忙。

这是代码。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html>
<head>
<title></title>


<script type="text/javascript">
/* <![CDATA[ */

function confirmPassword() 
{
    if (document.forms[0].password_confirm.value != document.forms[0].password.value) 
    {
        window.alert("You did not enter the same password!");
            document.forms[0].password.focus();
    }
}

function submitForm() 
{
    submitYesCancel();

    if (document.forms[0].name.value == "" 
        || document.forms[0].name.value == "Your Name") 
    {
        window.alert("You must enter your name.");
            return false;
    }

    else if (document.forms[0].emailAddress.value == ""
        || document.forms[0].emailAddress.value == "Your Email")
    {
        window.alert("You must enter your email address.");
            return false;
    }

    else if (document.forms[0].password.value == ""
        || document.forms[0].password_confirm.value == "")
    {
        window.alert("You must enter a password.");
            return false;
    }

    else if (document.forms[0].sq.value ==""
        || document.forms[0].sq.value == "Your Security Answer")
    {
        window.alert("You must enter a security answer.");
            return false;
    }

    radioB();

        return true;
}

function submitYesCancel()
{
    var submitForm = window.confirm("Are you sure you want to submit the form?");
        if (submitForm == true) 
        {
            return true;
            return false;
} 
}

function radioB()
{

    var radioButton = false;
        for (var i = 0; i < 4; ++i) 
        {
            if (document.forms[0].special_offers[i].checked == true) 
            {
        radioButton = true;
            break;
            }       
        }
        if (radioButton != true) 
        {
        window.alert("You must select a radio button.");
            return false;

        }
} 

function confirmReset() 
{
    var resetForm = window.confirm("Are you sure you want to reset the form?");
        if (resetForm == true)
            return true;
            return false;
}

/* ]]> */
</script>

</head>
<body>

<form>

<h2>Personal Information</h2>
    <p>Name:<br />
        <input type = "text" name = "name" placeholder = "Your Name" size = "50"/></p>
    <p>Email Address:<br />
        <input type = "text" name = "emailAddress" placeholder = "Your Email" size= "50" /></p>


<h2>Security Information</h2>
    <p>Please enter a password of 8 characters or less: <br />
        <input type = "password" name = "password" maxlength = "8" /></p>
    <p>Confirm password<br />
        <input type = "password" name = "password_confirm" size = "50" onblur = "confirmPassword();" /></p>

    <p>Please Select a Security Question from the Drop Down List.<br />
        <select name = "Security Question">
            <option value = "mother">What is your Mother's maiden name?</option>
            <option value = "pet">What is the name of your pet?</option>
            <option value = "color">What is your favorite color?</option>
        </select></p>
        <p><input type = "text" name = "sq" placeholder = "Your Security Answer" size = "50" /></p>


<h2>Preferences</h2>        
    <p>Would you like special offers sent to your email address?<br />
        <input type = "radio" name = "radioButton" value = "Yes" />Yes<br />
        <input type = "radio" name = "radioButton" value = "No" />No<br /></p>

    <p>Are you interested in special offers from: <br />
        <input type = "checkbox" name = "sCheckboxes" value = "e" />Entertainment<br />
        <input type = "checkbox" name = "sCheckboxes" value = "b" />Business<br />
        <input type = "checkbox" name = "sCheckboxes" value = "s" />Shopping<br /></p>

<button onclick="return submitForm();">Submit</button>
<button onclick="return confirmReset();">Reset</button>

</form>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

由于您的Javascript完全错误,它无效。

}
radioB();
else   // <--- what does it mean?
    return true;

else if (radioButton ! = true) {  
// <-- you have else if, but there is no if block and it is != not ! =

下次当您的Javascript无效时,请先尝试查看错误。您可以在Google Chrome中轻松完成此操作。点击Ctrl + Shift + J,转到控制台标签。然后,在遇到错误时修复每个错误,直到没有错误为止。

enter image description here