我有一個下拉列表,我使用.Change()來觸發函數。使用getJSON
功能基本得到一定的數據並且基於那些值必須爲mp3文件創建一個數組串。字符串變量的前綴爲undefined for循環
下面的代碼是生成字符串,但始終以字符串undefined
爲前綴。
在代碼中,你會注意到setTimeout
這只是提供一定的延遲,直到收到數據。在下面的例子中,我使用靜態值,它仍然是前綴undefined
。不知道爲什麼可能我以錯誤的方式定義了變量。
完整示例JSBin
$('.customSurah').change(function(){
//surahNo = $('#surah option:selected').val();
setTimeout(function(){
//countSpan = $('#surah-wrapper').children().length;
surahNo = 1;
countSpan = 7;
var i=0;
for (i = 0; i <= countSpan; i++) {
strCat += surahNo+"/"+i+".mp3,";
console.log(strCat);
}
}, 3000);
});
輸出
undefined114/0.mp3,
undefined114/0.mp3,114/1.mp3,
undefined114/0.mp3,114/1.mp3,114/2.mp3,
undefined114/0.mp3,114/1.mp3,114/2.mp3,114/3.mp3,
undefined114/0.mp3,114/1.mp3,114/2.mp3,114/3.mp3,114/4.mp3,
undefined114/0.mp3,114/1.mp3,114/2.mp3,114/3.mp3,114/4.mp3,114/5.mp3,
undefined114/0.mp3,114/1.mp3,114/2.mp3,114/3.mp3,114/4.mp3,114/5.mp3,114/6.mp3,
where'strCat' defined?用空白字符串聲明它的初始值,它應該工作正常 –
它被定義爲side for循環這裏是完整的例子http://jsbin.com/nicutokago/edit?html,output – Learning