如果我的術語不正確,我很抱歉 - 這絕對不是我的專業領域。列出按鍵分組的數組內容
我期待從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)。 –