简体   繁体   English

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

[英]onSubmit Function not able to call a function inside of it

I am trying to make a form. 我正在尝试制作表格。 I want it to check the radio buttons to see if they have been clicked, and if not to have a message to the user to check one. 我希望它检查单选按钮以查看是否已单击它们,如果没有,则向用户显示一条消息以进行检查。

I tried to just enter it, then I tried to continue my else if statements with it (got error messages), then I tried making a function within the onsubmit function (it simply didn't initiate), then I tried making a function outside of the onsubmit function and am trying to call it, but it does not initiate. 我尝试只输入它,然后尝试继续执行其他if语句(得到错误消息),然后尝试在onsubmit函数内创建一个函数(它根本没有启动),然后尝试在外部创建一个函数onsubmit函数,并试图调用它,但它不会启动。 I've even tried moving the functions on top or below the onsubmit function. 我什至尝试将函数移至onsubmit函数的上方或下方。

I made the submitYesCancel to see if the problem was with the radioB function, but neither function will initiate. 我进行了commitYesCancel来查看问题是否出在radioB函数上,但是这两个函数都不会启动。

I'm hopelessly stuck. 我无可救药地被困住了。 Please help. 请帮忙。

Here is the code. 这是代码。

<!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>

The reason that it does not work because your Javascript is completely wrong. 之所以无法运行,是因为您的Javascript完全错误。

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

And

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

Next time when your Javascript does not work, try to see the error first. 下次当您的Javascript不起作用时,请尝试首先查看该错误。 You can easily do this in Google Chrome. 您可以在Google Chrome浏览器中轻松完成此操作。 Hit Ctrl + Shift + J , go to Console tab. Ctrl + Shift + J ,转到“控制台”选项卡。 Then, fix each error when you encounter it until there is no more error. 然后,修复遇到的每个错误,直到没有其他错误为止。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM