本文介绍了preventing提交,直到ajaxes完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用jQuery,我想让这个表单提交将无法工作,直到所有的Ajax调用完成。

I'm using jQuery, and I'd like to make it so that a form submit won't work until all ajax calls have completed.

我以为这样做是将存储一个布尔值,该值指示是否有一个Ajax请求事情的一种方式。它将被设置为假,在每一个末端。

One way I have thought of doing this would be to store a boolean value which indicates whether there is an ajax request going on. It would be set to false at the end of every one.

我不知道这是否是虽然最好的办法,所以我最好的AP preciate任何输入。

I'm not sure if this is the best way though, so I'd appreciate any input.

推荐答案

而不是使用布尔,你应该使用一个整数计数器(变种i = 0; )。

Rather than use boolean, you should use an integer counter (var i=0;).

我++ 每一个AJAX请求时的时间,以及

i++ every time an AJAX request is made, and

我 - 一个AJAX请求完成每次

i-- every time an AJAX request is completed.

使用计数器的好处是,它可以让你做在同一时间多个AJAX请求。布尔,更越野车,因为使多个请求可能会导致提交按钮解锁动作完成之前。

The benefits of using a counter are that it will allow you to make multiple AJAX requests at a time. With boolean, it is more buggy, because making multiple requests can cause the submit button to unlock before actions are completed.

对于用户界面,您应该考虑使用某种形式的负荷的指标,表明它正在工作。然后,一旦计数器回零,您可以隐藏加载指示灯,使提交的功能。

As for the user interface, you should consider using some sort of 'loading' indicator, to show that it is working. Then, once the counter is back to zero, you can hide the loading indicator and enable the submit functionality.

这篇关于preventing提交,直到ajaxes完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 23:21