传递变量$阿贾克斯()()完成 [英] Passing variables to $.ajax().done()
本文介绍了传递变量$阿贾克斯()()完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我迷路了。我怎么可能会传递一个循环变量的AJAX .done()调用?
I'm lost. How might I pass a loop variable to an AJAX .done() call?
for (var i in obj) {
$.ajax(/script/).done(function(data){ console.log(data); });
}
显然,如果我做的的console.log(我+''+数据)
的我的将返回的最后关键的对象 OBJ
在每一个迭代。文档失败了我。
Obviously, if I were to do console.log(i+' '+data)
i would return the very last key in the object obj
on every single iteration. Documentation fails me.
推荐答案
您可以使用闭包(通过自执行的函数)来捕捉我
的每个值这样循环调用:
You can use a closure (via a self executing function) to capture the value of i
for each invocation of the loop like this:
for (var i in obj) {
(function(index) {
// you can use the variable "index" here instead of i
$.ajax(/script/).done(function(data){ console.log(data); });
})(i);
}
这篇关于传递变量$阿贾克斯()()完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文