jQuery提交/ preventDefault / post

时间:2013-06-21 19:19:12

标签: jquery

目标

  • 我正在尝试使用AJAX将数据(从“页面A”上的表单)发布到另一页(“页面B”)
  • 我希望将“Page B”中的某些内容加载到指定div中的“Page A” (#result)。

我尝试过什么

症状

  • 页面A直接提交给页面B,好像表格正常提交而且没有jQuery可以进行调解。

理论

  • .submit()方法未附加
  • 或它正在附加,但其中的preventDefault指令并未拦截传统的表单提交

HTML

<form action="/echo/html/" id="form_edit_sensitivity" method="post" onsubmit="trimTextFields(this); return checkForm(this);" class="form-horizontal">
    <div class="control-group">
        <label for="p_sensitivity_type_id" class="control-label">Group</label>
        <div class="controls">
            <select name="p_sensitivity_type_id" size="1" title="Sensitivity Type" id="p_sensitivity_type_id">
                <option value=""></option>
                <option value="1">Politician</option>
                <option value="2" selected="selected">Celebrity</option>
            </select>
        </div>
    </div>
    <div class="control-group">
        <label for="p_expiration_dte" class="control-label">Expiration Date</label>
        <div class="controls">
            <input type="date" name="p_expiration_dte" id="p_expiration_dte" value="" data-datepicker-value="" min="1789-07-29" />
        </div>
    </div>
    <div class="control-group">
        <div class="controls">
            <label for="p_super_sensitive" class="checkbox">
                <input type="checkbox" id="p_super_sensitive" value="Y" name="p_super_sensitive">Feel like checking this box?</label>
        </div>
    </div>
    <div class="form-actions">
        <input type="hidden" name="some_value" value="1">
        <input type="hidden" name="data" value="<p>Text echoed back to request</p>">
        <input type="submit" value="Submit" class="btn btn-primary">
        <input type="reset" value="Reset" class="btn">
    </div>
</form>
<div id="result">We want to load the results within this div</div>

的JavaScript

var trimTextFields = function () {
    alert('trim fields');
},
checkForm = function (incoming_form) {
    alert('custom validation');
};

/* attach a submit handler to the form */
$('#form_edit_sensitivity').submit(function (event) {

    /* stop form from submitting normally */
    event.preventDefault();

    confirm('we got this far');
    /* get some values from elements on the page: */
    var $form = $(this),
        data = $form.serialize(),
        url = $form.attr('action');

    /* Send the data using post */
    var posting = $.post(url, data);

    /* Put the results in a div */
    posting.done(function (data) {
        var content = $(data).find('#summary');
        $('#result').empty().append(content);
    });
});

为了解释the immortal words of the "Deranged Sorority Girl",我准备将C-Punt放在这台计算机上。

再次,jsFiddle at http://jsfiddle.net/jhfrench/QjaTq/

2 个答案:

答案 0 :(得分:3)

var trimTextFields = function () {
    alert('trim fields');
},
checkForm = function (incoming_form) {
    alert('custom validation');
    return true; // <-- NECESSARY BECAUSE YOU'RE RETURNING THIS IN "submit" EVENT
};

$(document).ready(function() // DOM is ready...
{
    /* attach a submit handler to the form */
    $('#form_edit_sensitivity').submit(function (event)
    {
        /* stop form from submitting normally */
        event.preventDefault();

        confirm('we got this far');
        /* get some values from elements on the page: */
        var $form = $(this),
            data = $form.serialize(),
            url = $form.attr('action');

        /* Send the data using post */
        var posting = $.post(url, data, function(response)
        {
            /* Put the results in a div */
            var content = $(response).find('#summary');
            $('#result').empty().append(content);
        });
    });
});

我不知道如何使用trimTextFields和checkForm(可能是全局),但如果可以,请在$(document).ready()中声明它们。始终尽量避免全局变量。

答案 1 :(得分:2)

您需要将js包装在ready handler -

$(function(){
   // write your js here
});

演示---> http://jsfiddle.net/QjaTq/2/