我有一个下拉列表,我使用.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