简体   繁体   English

HTML中的提交按钮未调用Javascript

[英]Javascript isn't being called by submit button in HTML

For some reason my javascript won't execute when my submit button is pressed. 由于某些原因,当按下我的提交按钮时,我的javascript无法执行。 It's supposed to leave an error message beside two textfields for a user's name and email address if they are empty, but it's not and I can't figure out why. 如果用户名和电子邮件地址为空,则应该在两个文本字段旁边留下错误消息,但不是,我也不知道为什么。 Normally, when the two boxes are filled, the submit would go to a php page which sends an email. 通常,当两个框都填满时,提交将转到发送电子邮件的php页面。 Any suggestions or help with fixing my javascript problem will be greatly appreciated. 任何建议或帮助解决我的JavaScript问题将不胜感激。

This is my javascript: 这是我的javascript:

//my javascript function
<script type='text/javascript'>
function validate_form()
{
        $('span.error_message').html('');
        var success = true;
        $("#validate_form input").each(function()
        {
            if($(this).val()=="")
            {
                $(this).next().html("* You must complete this field"); // the error message
                success = false;
            }
        });
        return success;
    }
</script>

and my form in html: 和我的形式在HTML:

<form action=".....whatever....." method="POST" id="validate_form" onsubmit="return validate_form();">
    <ol>
        <li>
            <span id="question">___ is your name? My name is Marie.</span>
            <input type="text" name="q1" id="q1" />
        </li>
        <li>
            <span id="question">___ are you from? I'm from Paris, France.</span>
            <input type="text" name="q2" id="q2" />
        </li>
        <li>
            <span id="question">Dave: ___ you like football</span>
            <p>
                <input type="radio" name="q6" value="1" id="q6_1" />
                <label for="q6_1">Are</label>

                <input type="radio" name="q6" value="2" id="q6_2" />
                <label for="q6_2">Do</label>

                <input type="radio" name="q6" value="3" id="q6_3" />
                <label for="q6_3">Does</label>

                <input type="radio" name="q6" value="4" id="q6_4" />
                <label for="q6_4">Is</label>
            </p>
        </li>
    <div id="username">
        <p>
            Before we begin, please enter your name and email:
            <br />
            <label for="user_name">Name: </label><input type="text" name="user_name" id="user_name" />
                <span class="error_message" style="color:#FF0000"></span>
            <br />
            <label for="user_email">Email: </label><input type="text" name="user_email" id="user_email" />
                <span class="error_message" style="color:#FF0000"></span>
        </p>
    </div>

        <p style="width: 242px; margin: auto;">
            &nbsp;
            <input type="submit" name="submit" value="Submit your answers" id="but" />
        </p>
</form>

Edit: Fixed the problem with the javascript being called, but now the submit button isn't working even with the two textfields being filled. 编辑:修复了JavaScript被调用的问题,但是即使两个文本字段都被填充,提交按钮也无法正常工作。

$("#validate_form input") - I don't see validate_form anywhere; $("#validate_form input") -在任何地方都看不到validate_form you need to add it or just use: $("input") 您需要添加它或只使用: $("input")

Firebug for firefox would help you debug these types of jscript issues. Firebug的Firebug将帮助您调试这些类型的jscript问题。 You can also use the Error Console in Firefox if you dont want to install firebug. 如果您不想安装Firebug,也可以在Firefox中使用错误控制台。

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

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