简体   繁体   English

通过AJAX发送表单时出错

[英]Error sending a form via AJAX

I am very new to jQuery and I'm looking for an explanation as to why this code does not seem to work. 我对jQuery非常陌生,我正在寻找有关为什么此代码似乎无法正常工作的解释。 I think it is something with the "action" not sure. 我认为这与“行动”并不确定。 Can someone help me understand my mistake here. 有人可以帮我了解我的错误吗? thanks 谢谢

<script src="/jquery.validationEngine.js"></script>
<script>
    $("#contact_body").submit(function(e) {
        e.preventDefault(); // Prevents the page from refreshing

        var $this = $(this); // `this` refers to the current form element

        if ($("#contact_body").validationEngine('validate')) {
            //Post Data to Node Server
            $.post(
                $this.attr("action"), // Gets the URL to sent the post to
                $this.serialize(), // Serializes form data in standard format
                function(data) { /** code to handle response **/ },
                "json" // The format the response should be in
            );

            //Notify User That the Email Was Sent to the Server & Thanks!
            //$('#contactThanksModal').modal('show');
            $('#contactModal').modal('hide');
            alert("success"); 
        }
        else {
            //handle Invalid Email Format Error
            alert("error"); 
        }
    });
</script> 
<!--pop up contact form -->
<div id="contact" class="modal hide fade in" style="display: none;">
    <div class="modal-header">
        <a class="close" data-dismiss="modal">x</a>
        <h3>Send us a message</h3>
    </div>
    <div class="modal-body">

    <form id="contact_body"class="contact_body" name="contact_body" action="/contact">
        <label class="label" for="form_name">Your Name</label><br>
        <input type="text" name="form_name" id="form_name" class="input-xlarge"><br>

        <label class="label" for="form_email">Your E-mail</label><br>
        <input type="form_email" name="form_email" class="input-xlarge"><br>

        <label class="label" for="form_msg">Enter a Message</label><br>
        <textarea name="form_msg" class="input-xlarge"></textarea>
    </form>
</div>

<div class="modal-footer">
    <input class="btn btn-success" type="submit" value="Send!" id="submit">
    <a href="#" class="btn" data-dismiss="modal">Nah.</a>
</div>

<!-- <div id="thanks"><p><a data-toggle="modal" href="#contact" class="btn btn-primary btn-large">Modal powers, activate!</a></p></div> -->

You need to wrap your JQuery scripts in a 您需要将JQuery脚本包装在

$(document).ready(function() {
    ...your_code_here...
});

This will then wait for the whole document to load before trying to attach events to objects. 然后,这将等待整个文档加载,然后再尝试将事件附加到对象。

Without this you may be trying to bind events to objects that have yet to be "created". 否则,您可能试图将事件绑定到尚未“创建”的对象。

You need to put your code in a document ready handler: 您需要将代码放入文档就绪处理程序中:

<script src="/jquery.validationEngine.js"></script>
<script>
    $(function() {
        $("#contact_body").submit(function(e) {
            // your code...
        });
    });
</script> 

Your code is currently trying to add the submit() handler before the element exists in the DOM. 您的代码当前正在尝试在DOM中存在该元素之前添加submit()处理程序。

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

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