for(var i =0; i < featuredItems.length; i++) {
var item = featuredItems.eq(i).text().trim();
alert(item); // alerts correctly
$.ajax({
url : "/documents/ajax/" + item,
dataType : "text",
success : function(data) {
/*
alerts the value set in the last iteration of for loop.
I want the same value as value of item in the ajax request URL
*/
alert (item);
},
});
}
如何在成功处理程序中正确访问var项的值?
上面的代码警告
答案 0 :(得分:0)
您必须关闭item
值:
for(var i =0; i < featuredItems.length; i++) {
void function(item) {
$.ajax({
url : "/documents/ajax/" + item,
dataType : "text",
success : function(data) {
alert (item);
},
});
}(featuredItems.eq(i).text().trim());
}