如何将ajax放入循环中? [英] How to put ajax inside a loop?
本文介绍了如何将ajax放入循环中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在循环内的ajax请求中有一个ajax请求.在这里:
I have an ajax request within an ajax request that is inside a loop. Here it is:
for (var i = 0; i < input_files.length; i++) {
$.ajaxFileUpload({
url : dasdasdsa,
success : function () {
$.ajax({
var get_the_number_in_the_last_of_array = input_files[i].split('_').pop();
});
}
});
}
放入async: false
无效.如何解决这个问题?我想要的是内部ajax的每个循环都获取正确的变量i
.我的代码中发生的事情是内部ajax始终获取最后一个i
.
putting async: false
doesnt work. How to go around this? What i want is that every loop the inner ajax gets the right variable i
. What happens in my code is that the inner ajax always gets the last i
.
注意:变量input_files
是一个包含ID的数组,例如"my_id_number_9".最后那9个是我想要获得的.
Note: variable input_files
is an array containing ids, example "my_id_number_9". That 9 in the last is what i want to get in the split.
推荐答案
使用closure
传递正确的i
-
for (var i = 0; i < input_files.length; i++) {
(function (index) {
$.ajaxFileUpload({
url : dasdasdsa,
success : function () {
$.ajax({
var get_the_number_in_the_last_of_array = input_files[index]
.split('_').pop();
});
}
});
})(i);
}
这篇关于如何将ajax放入循环中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文