ajaxSubmit和响应

时间:2011-07-15 06:49:12

标签: ajax jquery ajaxform

我正在尝试使用ajaxSubmit事件,因此表单将替换为结果。该结果可能是表单上的确认页面或错误。这个表单在它自己的页面上工作,所以我正在努力将它放在一个ajax模式中。

<div id="biography">
    <form method="post" action="/{{ onesheet.url }}/contact/" id="contact-form" class="full">
        {% csrf_token %}
        <ul>
        {{ form.as_ul }}
        </ul>
        <input type="submit" value="Send Email">
    </form>
</div>

和js

<script type="text/javascript">
    $('#contact-form').submit(function() { 
        var options = { 
            target:        '#biography',   // target element(s) to be updated with server response 
            beforeSubmit:  showRequest,  // pre-submit callback 
            success:       showResponse  // post-submit callback 
        };
        // submit the form 
        $(this).ajaxSubmit(options);
        // return false to prevent normal browser submit and page navigation 
        return false; 
    });
</script>

但这并没有在目标中加载响应。它在整个页面中打开它。

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

您需要致电e.preventDefault()以阻止正常提交:

<script type="text/javascript">
$('#contact-form').submit(function(e) { 
    e.preventDefault();
    var options = { 
        target:        '#biography',   // target element(s) to be updated with server response 
        beforeSubmit:  showRequest,  // pre-submit callback 
        success:       showResponse  // post-submit callback 
    };
    // submit the form 
    $(this).ajaxSubmit(options);
    // return false to prevent normal browser submit and page navigation 
    return false; 
});
</script>