jQuery Ajax没有返回成功

时间:2011-09-26 01:10:43

标签: jquery ajax

我有这个代码转到form.php而不是将回显的php拉到我主页上的#success div中:

<script type="text/javascript">

    data = $(form).serialize();
    $('#form').submit(function() {   
        $.ajax({
        url: '../wp-content/themes/MC/form.php',
        type: 'POST',
        data: data,
        success: function(result) {
            $('#success').html('').html(result);
            }
        });
    });
</script>
                <div id="form">
                <br>
                <form action="http://www.mcfilmmakers.com/wp-content/themes/MC/form.php" method="post">
                    Name / Nom:<input type="text" name="fullname" /><br />
                    E-mail / Courriel:<input type="text" name="email" /><br />                          Your Daily URL / Votre URL Quotidien:<input type="text" name="link" /><br />
                    Voting Instructions / Instructions de Vote:<textarea name="instr" style="width: 450px; height: 100px;"/></textarea><br />
                    <input type="submit" value="Submit" />
                </form>
            </div>
            <div id="success">
            </div>

form.php会收到帖子信息,因为它正在回显输入。出于某种原因,它只是没有插入#success。

2 个答案:

答案 0 :(得分:1)

试试这个:

$('#form').submit(function(event) {   
    $.ajax({
        url: '../wp-content/themes/MC/form.php',
        type: 'POST',
        data: data,
        success: function(result) {
            $('#success').html(result);
        }
    });
    event.preventDefault();
});

提交表单时,默认情况下会在表单中发布表单的action属性。您希望阻止该帖子发生,而是使用ajax。因此event.preventDefault()会阻止这种情况发生。

也不需要链接.html('')因为它设置了整个内容。见http://api.jquery.com/html/

答案 1 :(得分:1)

您的代码:

$('#form').submit(function()

#form points to `div id="form"`

您向div注册onsubmit事件。 尝试像这样添加id=form

<form action="http://www.mcfilmmakers.com/wp-content/themes/MC/form.php" method="post" id="form">

并从div中删除id="form"

相关问题