Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/88.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript onSubmit函数无法调用其中的函数_Javascript_Html_Function_Onsubmit - Fatal编程技术网

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

Javascript onSubmit函数无法调用其中的函数,javascript,html,function,onsubmit,Javascript,Html,Function,Onsubmit,我正在做一个表格。我想让它检查单选按钮,看看他们是否被点击,如果没有有一个消息给用户检查一个 我试着只输入它,然后我试着继续我的else if语句,其中有错误消息,然后我试着在onsubmit函数中创建一个函数,它根本没有启动,然后我试着在onsubmit函数之外创建一个函数,并试图调用它,但它没有启动。我甚至尝试将函数移动到onsubmit函数的上方或下方 我取消了提交,看看问题是否出在radioB功能上,但这两个功能都不会启动 我陷入了绝望。请帮忙 这是代码 <!DOCTYPE htm

我正在做一个表格。我想让它检查单选按钮,看看他们是否被点击,如果没有有一个消息给用户检查一个

我试着只输入它,然后我试着继续我的else if语句,其中有错误消息,然后我试着在onsubmit函数中创建一个函数,它根本没有启动,然后我试着在onsubmit函数之外创建一个函数,并试图调用它,但它没有启动。我甚至尝试将函数移动到onsubmit函数的上方或下方

我取消了提交,看看问题是否出在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>

它之所以不能工作,是因为您的Javascript完全错误

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

下次当Javascript无法工作时,请先尝试查看错误。你可以在谷歌Chrome上轻松做到这一点。按Ctrl+Shift+J,转到控制台选项卡。然后,在遇到错误时修复每个错误,直到不再有错误为止


将侦听器放在表单的提交处理程序上,而不是按钮上,因为表单可以在不单击提交按钮的情况下提交。好的,我已经完成了您的建议。我下载了Google Chrome并编辑了我的代码,直到没有错误。我仍然无法使这些函数工作。我把编辑过的代码放在我的第一篇文章中。如果你还有什么建议,请告诉我。
else if (radioButton ! = true) {  
// <-- you have else if, but there is no if block and it is != not ! =