如果我的术语不正确,我很抱歉——这绝对不是我的专业领域。
我想从一个json文件中制作一个<select>
列表,并通过一个键将条目分组在<optgroup>
中。 我已经成功地能够列出选择中的所有条目,但不知道如何循环并将项目嵌套在其键下。
我的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
是一个数组,你应该看一下这个问题及其答案。 - Heretic Monkey