7

有什么想法可以将 JQuery 的延迟方法与检测所有更改的表单并将每个表单作为 Ajax 帖子提交的功能一起使用吗?

如果我只列出大量表单提交,我可以得到同样的工作,但如果我使用......

$('form.changed').each(function(){
  return $(this).submitWithAjax();
});

我正在尝试使用的代码的更完整版本在这里...... 在 JS Fiddle

提前致谢!

4

2 回答 2

17

代替“.each()”,使用“.map()”:

var deferreds = $('form.changed').map(function(i, elem) {
  return $(this).submitWithAjax();
});

$.when.apply(null, deferreds.get()).then(function() { ... });

“$.when()” 可以让你捆绑一堆延迟对象并等待它们全部成功(或任何失败——注意那里的区别)。它通常允许任意数量的参数,但是因为我们有一个数组,所以我使用了“apply()”。

请注意,我只是轻轻地使用了这些东西,因此请阅读 jQuery API 文档以仔细检查 :-)编辑——同样在重新阅读您的问题后,我可能误解了您。

于 2011-05-28T16:40:04.867 回答
0

Delegating the change event to the form fields could solve your problem here.

$('form').delegate('input[type=text], input[type=radio], select', 'change', 
function(evt){
    // your submits here
    console.log('changed!')
});
于 2011-05-28T16:46:33.947 回答