jQuery的AJAX表单提交不工作 [英] jQuery AJAX form submission not working

查看:125
本文介绍了jQuery的AJAX表单提交不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过AJAX提交使用正常POST提交的表单。的HTML是只是一个标准的形式方法=后和我的jQuery的code是如下:

I'm trying to submit a form through AJAX instead of using the normal POST submission. The HTML is just a standard form with method="post" and my jQuery code is as follows:

jQuery.noConflict();
jQuery(document).ready( function($) {
    var $form = $('#default_contact');

    $form.submit( function() {
        $.ajax({
            type: 'POST',
            url: $form.attr( 'action' ),
            data: $form.serialize(),
            success: function( response ) {
                console.log( response );
            }
        });

        return false;
    });
});

(基于<一href="http://stackoverflow.com/questions/425095/submit-form-using-ajax-and-jquery/425142#425142">this答案)

我返回false从提交功能,但形式仍然得到提交,我想不出为什么。没有得到任何Firebug的错误。

I'm returning false from the submit function, but the form is still getting submitted and I can't figure out why. Not getting any Firebug errors.

推荐答案

您code工作正常,如果HTML是设置正确。这里是我的测试HTML(用PHP),这样你可以比较你自己的。

your code works fine if html is setup right. here's my test html (with php), so you can compare to your own.

<?php
if (!empty($_POST)) {
  die("<pre>" . print_r($_POST, true) . "</pre>");
}
?>
<html>
<head>
  <title>ajax submit test</title>
<script type="text/javascript" src="jquery-1.3.2.min.js"></script>
<script type="text/javascript">
    jQuery.noConflict();
    jQuery(document).ready(function ($) {
        var $form = $('#default_contact');

        $form.submit(function () {
            $.ajax({
                type: 'POST',
                url: $form.attr('action'),
                data: $form.serialize(),
                success: function (response) {
                    alert(response);
                }
            });

            return false;
        });
    });
</script>      
</head>
<body>
  <form id="default_contact" action="formsubmit.php" method="post">
    <input type="hidden" value="123" name="in1" />
    <input type="hidden" value="456" name="in2" />
    <input type="submit" value="send" />
  </form>
</body>
</html>

这篇关于jQuery的AJAX表单提交不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆