如果我的术语不正确,我很抱歉 - 这绝对不是我的专业领域。列出按键分组的数组内容
我期待从json文件中创建一个<select>
列表,并通过键将<optgroup>
s中的条目分组。我已经成功地列出了选择中的所有条目,但不知道如何循环并嵌套项目。
我的JSON是这样的:
[
{
"Type" : "Overdrive",
"Brand" : "Chase Bliss",
"Name" : "Brothers",
"Width" : 2.75,
"Height" : 4.77,
"Image" : "public/images/pedals/chasebliss-brothers.png"
}
]
这里是我正在渲染<select>
:
window.RenderPedals = function(pedals){
for(var i in pedals) {
var $pedal = $("<option>"+ pedals[i].Brand + " " + pedals[i].Name +"</option>").attr('id', pedals[i].Name.toLowerCase().replace(/\s+/g, "-").replace(/'/g, ''));
$pedal.data('width', pedals[i].Width);
$pedal.data('height', pedals[i].Height);
$pedal.data('height', pedals[i].Height);
$pedal.data('image', pedals[i].Image);
$pedal.appendTo('.pedal-list');
}
}
我所寻找的是这样的标记:
<select>
<optgroup label="Chase Bliss">
<option data-width="..." data-height="..." data-image="...">Chase Bliss Brothers</option>
</optgroup>
</select>
能任何人都指向正确的方向?
如果'pedals'是一个数组,你应该看看[这个问题,它的答案(http://stackoverflow.com/questions/500504/why-is-using - 对于功能于与阵列迭代-A-坏主意?RQ = 1)。 –