jQuery提交/ preventDefault / post [英] jQuery submit/preventDefault/post

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

问题描述

目标


  • 我正在尝试使用AJAX发布数据(来自页面上的表单一个)到另一个页面(Page B)

  • 我希望Page B中的一些内容被加载到指定div中的Page A (#result)。

  • I am trying to use AJAX to post data (from a form on "Page A") to another page ("Page B")
  • I want some content from "Page B" to be loaded into "Page A" within a specified div (#result).

我尝试过的事情

  • I've created the relevant HTML and JS largely-based on the jQuery .post() documentation with some influence from jQuery AJAX submit form.
  • I've tinkered with alert() to see where the process is failing.
  • I've created a jsFiddle at http://jsfiddle.net/jhfrench/QjaTq/ (the HTML and JavaScript that follow are the same)

症状


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

理论


  • .submit ()方法没有附加

  • 或者它正在附加但是 preventDefault 指令并没有拦截传统表单提交

  • the .submit() method is not attaching
  • or it is attaching but the preventDefault directive within is not intercepting the traditional form submission

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);
    });
});

解释疯狂女生联谊会女孩的不朽言论,我准备好C-Punt我的电脑了一个。

To paraphrase the immortal words of the "Deranged Sorority Girl", I am about ready to C-Punt my computer over this one.

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

推荐答案

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()里面。总是尽量避免全局变量。

I don't know how you pan to use trimTextFields and checkForm (probably globally), but declare them inside $(document).ready() if you can. Always try to avoid global variables.

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

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