如何使用 jQuery 表单插件更新 beforeSubmit 函数中的选项对象

发布于 2024-09-08 23:39:02 字数 1932 浏览 3 评论 0 原文

我正在重构我对 $.ajax 的使用,以使用 jQuery 表单插件提交表单。 API 声明可以传递任何传递给 的选项$.ajax$.ajaxSubmit。我希望修改 beforeSubmit 中的选项对象,添加 data 属性。提醒 processSubmitoptions.beforeSubmit 的值表明这是由处理程序初始化的选项对象并且数据已设置,但由于选项对象已经初始化如果没有 data 属性,它不会包含在发送到服务器的帖子中。是否可以在 beforeSubmit 中或以其他方式修改选项对象?

文档准备好后,我将处理程序绑定到 submit()

$("#myform").submit(function() {
    var options = { dataType: 'json',
            beforeSubmit: processSubmit,
            success: endSubmit };
    $(this).ajaxSubmit(options);
    return false;
});

processSubmit(arr, $form, options) 函数将数据打包为 JSON 发送到服务器:

function processSubmit(arr, $form, options) {
    var followers =[];
    $($("#follower-multi-select").children().filter("li")).each(function() {
      if ($(this).hasClass("selected")) {
        var fwr =  $(this).attr("id");
        followers.push(fwr);
      }
    });

    var postData = { followers : followers };
    alert('beforeSubmit is: ' + options.beforeSubmit);

    options.data = 'json='+$.toJSON(postData);
    alert('data set: ' + options.data);
}

表单 HTML:

<form id="myform" action="/myaction" method="post">
    <fieldset>
        <legend>Choose your competitors</legend>
        <ul id="follower-multi-select" class="follower-multi-select">
            {% for f in follower_thumbs %}
            <li id="{{ f.hashedkey }}"><img src='{{ f.thumb }}' alt="{{ f.name }}" /><span class="name-multi-select">{{ f.name }}</span></li>
            {% endfor %}
        </ul>
    </fieldset>
    <input id="send" type="submit" class="large" value="send" />
</form>

I am refactoring my use of $.ajax to submit forms using the jQuery Forms Plugin. The API states that it is possible to pass any of the options passed to $.ajax to $.ajaxSubmit. I wish to modify the Options Object in the beforeSubmit, adding a data property. Alerting the value of options.beforeSubmit in processSubmit suggests that this is the Options Object initialised by the handler and that the data is set, but as the Options Object has already been initialised with no data property, it isn't included in the post sent to the server. Is it possible to modify the Options Object in the beforeSubmit, or in some other way?

When the document is ready I bind a handler to submit():

$("#myform").submit(function() {
    var options = { dataType: 'json',
            beforeSubmit: processSubmit,
            success: endSubmit };
    $(this).ajaxSubmit(options);
    return false;
});

The processSubmit(arr, $form, options) function packages up data to be sent to the server as JSON:

function processSubmit(arr, $form, options) {
    var followers =[];
    $($("#follower-multi-select").children().filter("li")).each(function() {
      if ($(this).hasClass("selected")) {
        var fwr =  $(this).attr("id");
        followers.push(fwr);
      }
    });

    var postData = { followers : followers };
    alert('beforeSubmit is: ' + options.beforeSubmit);

    options.data = 'json='+$.toJSON(postData);
    alert('data set: ' + options.data);
}

The form HTML:

<form id="myform" action="/myaction" method="post">
    <fieldset>
        <legend>Choose your competitors</legend>
        <ul id="follower-multi-select" class="follower-multi-select">
            {% for f in follower_thumbs %}
            <li id="{{ f.hashedkey }}"><img src='{{ f.thumb }}' alt="{{ f.name }}" /><span class="name-multi-select">{{ f.name }}</span></li>
            {% endfor %}
        </ul>
    </fieldset>
    <input id="send" type="submit" class="large" value="send" />
</form>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼趣 2024-09-15 23:39:02

您应该使用 beforeSerializebeforeSubmit > 选项,因此您的数据在序列化时会被包含在内。您没有在传递给 $.ajax() 的选项上设置 .data,而是在 上设置 .data $.ajaxSubmit() 选项,这是序列化时要包含的额外数据插件的属性。当 beforeSubmit 发生时,序列化已经已经完成,你需要在此之前介入,就像这样:

$("#myform").submit(function() {
    var options = { dataType: 'json',
            beforeSerialize: processSubmit,
            success: endSubmit };
    $(this).ajaxSubmit(options);
    return false;
});

function processSubmit($form, options) {
    var followers = $("#follower-multi-select > li.selected").map(function() {
        return this.id;
    }).get();

    var postData = { followers : followers };
    alert('beforeSubmit is: ' + options.beforeSubmit);

    options.data = 'json='+$.toJSON(postData);
    alert('data set: ' + options.data);
}

我也使用了 .map()> child-selector 在这里使数组的获取变得更加简单,但是你原来的方法是有效的......只需注意重要的区别,回调中的参数与 beforeSubmit 不同,beforeSerialize 回调没有 arr 参数。

Instead of beforeSubmit you should use the beforeSerialize option, so your data gets included when it's serialized. You're not setting .data on the options passed to $.ajax(), you're setting .data on the $.ajaxSubmit() options, which is a property on the plugin of extra data to include when it serializes. When beforeSubmit happens, the serialization has already been completed, you need to step in before that, like this:

$("#myform").submit(function() {
    var options = { dataType: 'json',
            beforeSerialize: processSubmit,
            success: endSubmit };
    $(this).ajaxSubmit(options);
    return false;
});

function processSubmit($form, options) {
    var followers = $("#follower-multi-select > li.selected").map(function() {
        return this.id;
    }).get();

    var postData = { followers : followers };
    alert('beforeSubmit is: ' + options.beforeSubmit);

    options.data = 'json='+$.toJSON(postData);
    alert('data set: ' + options.data);
}

I also used .map() and the > child-selector here to make the getting of your array much simpler, but your original method works...just note the important difference that the parameters are different on the callback from beforeSubmit, there's no arr parameter for the beforeSerialize callback.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文