好的第一件事json结构
[{
"type": "button",
"name": "Off",
"tabname": "1",
"image_file": "path\\Off.gif"
}, {
"type": "button",
"name": "Off1",
"tabname": "2",
"image_file": "path\\Off1.gif",
"image_file_1": "path\\On1.gif"
}, {
"type": "button",
"name": "Off2",
"tabname": "3",
"image_file": "path\\Off2.gif",
"image_file_1": "path\\On2.gif",
"image_file_2": "path\\half.gif"
}
]
image_file 字段可以有多个条目(即 image_file、image_file_1、image_file_2 等),我如何在循环中动态访问它?
当前不工作的代码(只是相关的东西)
$.each(data, function (i, item) {
var images = [];
imageIndex = 1;
continueLoop = true;
while(continueLoop) {
if(imageIndex == 1) {
images.push(data[i].image_file);
}
else {
var testVal = 'image_file_' + imageIndex;
alert(data[i][testVal]);
if(data[i][testVal] === undefined) {
continueLoop = false;
}
else {
images.push(data[i][testVal]);
}
}
imageIndex++;
}
});
第一次迭代工作正常(if (imageIndex == 1)
即位),但else
我为测试值而输入的子句 alert 总是返回 undefined
任何帮助将不胜感激