jQuery的阿贾克斯()异步假 [英] jquery ajax() async false
问题描述
我有问题。
for(a=1;a<10;a++){
$(".div").append("<div id="+a+"></div>")
$.ajax({
url: "file.php",
data: "a="+a,
type: "POST",
async: false,
success: function(data) {
$("#"+a).html(data);
}
});
}
$("div").click(function(){
alert("it works");
});
问题是:
是我没有把有异步:从假
数据的为file.php 的仅在过去的div所以用ID 9
但现在有异步:假
- 这样的数据在每一个div中,这样的好
problem is:
is I didn't put there async: false
data from file.php are only in last div so with id 9
but now there is async: false
- so data are in every div so that is good
但是,如果我想点击,而它加载阿贾克斯不工作(仅限于完成后,所有的Ajax-ES)
but if i want click while it was loading by ajax it doesn't work (only after finished all ajax-es)
如何解决这个问题呢? (也许我使用AJAX的错误是,我可以使用的getJSON等..)
how resolve this problem? (maybe the false is that am I using ajax. I can use getJSON ect..)
感谢您帮助
推荐答案
如果您希望用户能够使用该接口Ajax调用运行时,你应该改变你的异步
到真
。也有人指出,由詹姆斯·阿勒代斯,在这种情况下,你需要使用JavaScript闭包来保留原始 ID
的值时,返回Ajax调用。有关JavaScript的闭包的更多信息,请查看<一个href="http://stackoverflow.com/questions/111102/how-do-javascript-closures-work">how-do-javascript-closures-work,发现这里的计算器一个非常好的问题。
If you want the user to be able to use the interface while the ajax call is running, you should change your async
to true
. It has also been noted by James Allardice that in this scenario you need to use a javascript closure to retain the value of your original id
for when the ajax call is returned. For more information regarding javascript closures check out how-do-javascript-closures-work, a really good question found here on stackoverflow.
for(id = 1; id < 10; id++){
$(".div").append("<div id='" + id + "'></div>");
(function(id) {
$.ajax({
url: "file.php",
data: "a=" + id,
type: "POST",
async: true,
success: function(data) {
$("#"+ id).html(data);
}
});
}(id));
}
这篇关于jQuery的阿贾克斯()异步假的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!