在jQuery中发布/提交多个表单 [英] Posting/submitting multiple forms in jQuery

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

问题描述

我在页面上有两个表单(一个是由AJAX抓取的)。我需要它们都被发布,所以我序列化第二个并使用jQuery在一个帖子中发送它,然后再提交第一个。但是,只有第一个表单似乎已发布。奇怪的是,如果我在序列化的帖子之后发出警报它按预期工作。这到底是什么问题?

I have two forms on a page (one is being grabbed by AJAX). I need them both to be posted, so I serialize the second one and send it in a post using jQuery before submitting the first one. However, only the first form appears to be getting posted. The odd thing is though, if I put an alert after the serialized post it works as expected. What exactly is my problem here?

$("#submit").livequery('click', function() {
    var form = $("#second_form");
    var action = form.attr("action");
    var serialized_form = form.serialize();
    $.post(action, serialized_form);

    //alert('test');

    $("#first_form").submit();

    return false;
});

有没有更好的方式发布两个表单,然后按下按钮重定向到新页面?我尝试使用上面的方法发布它们,然后使用window.location更改页面,但我遇到了类似的问题。

Is there any better way to post two forms and then redirect to a new page on a button press? I tried posting both of them using the method above and then using window.location to change pages but I had a similar problem.

非常感谢!

推荐答案

您可以尝试在second_form帖子的回调中提交first_form。我相信first_form的提交正在卸载页面,导致第二个帖子被中止。在second_form的回调中执行first_form的帖子将确保在第二个帖子开始之前完成初始帖子。

You might try submitting "first_form" in a callback from the post of "second_form". I believe that the submit of "first_form" is unloading the page which causes the second post to be aborted. Doing the post of "first_form" in the callback from "second_form" will ensure that the initial post is complete before the second post begins.

$("#submit").livequery('click', function() {
    var form = $("#second_form");
    var action = form.attr("action");
    var serialized_form = form.serialize();
    $.post(action, serialized_form, submit_first);
});

function submit_first(val) {
   $("#first_form").submit();
}

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

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