2013-03-14 59 views
0

我有一些情况,在同一页面上有多个表单,我需要通过单击“保存”按钮通过ajax提交。我使用$ .each循环遍历表单,然后发出ajax请求。 ajax请求应该发回一个响应(错误/成功消息)。通过AJAX提交多个表单 - 同步或异步

我的问题是 - 我会因为使用ASYNC请求而犯错误,因为匹配哪个输出到哪个表单可能有问题?你们有什么感想 ?

+0

切勿使用同步XHR请求。 – josh3736 2013-03-14 20:00:58

+0

他说什么^^^^^^! – adeneo 2013-03-14 20:05:21

+0

我在这里错过了什么?异步请求在匹配哪个输出时会遇到问题,因为异步请求不会等待完成。 – Ashesh 2013-03-14 20:08:27

回答

0

切勿使用同步XHR请求;没有合理的理由来使用它们,并导致糟糕的用户体验。

您不必担心混淆了的请求:

$('form').each(function() { 
    var form = $(this); 
    $.post(form.attr('action'), form.serialize(), function(r) { 
     // `form` is still the particular form submitted, 
     // and `r` will be the results of posting that form. 
    }); 
});