我有兩個例子。請告訴我爲什麼我的變量help
在這個例子中沒有按照預期工作。我檢查它正在進入循環。
結果:undefined
function autopop(){
var help;
$.ajax({
type : "POST",
url : "/cgi-bin/my.pl",
data : "action=autopop",
dataType : "json",
success : function(data) {
for (var i = 0; i < data.length; i++) {
help = "test";
}
}
);
$("#id").append(help);
}
結果:test
function autopop() {
var help = "test";
$.ajax({
type : "POST",
url : "/cgi-bin/my.pl",
data : "action=autopop",
dataType : "json",
success : function(data) {
for (var i = 0; i < data.length; i++) {
help = "blub";
}
}
);
$("#id").append(help);
}
請告訴我爲什麼我不能從這個AJAX /循環組合中,以及如何我可以改變這個事實訪問我的變種。
這是由於AJAX請求的異步性質,與其他1,092,938,932,893次相同。 –
@tymeJV是對的。關鍵是要記住它是異步的。如果你不知道這意味着什麼,請快速閱讀。 – j0hnstew
我不認爲有必要降低這一點。至少他已經把他嘗試過的並且正在尋求幫助。這比其他數以百萬計的「給我答案」的帖子要好得多。 –