Javascript 将数组追加到optgroup的jQuery正在跳过第一个选项
我有一个JSON对象,如下所示:Javascript 将数组追加到optgroup的jQuery正在跳过第一个选项,javascript,jquery,each,option,optgroup,Javascript,Jquery,Each,Option,Optgroup,我有一个JSON对象,如下所示: {"versions":{ "stable":["1.0","1.1"], "work_in_progress":["wip"], "branches":["branch1.0","branch1.1"] } 通过jQuery,我试图将这些数据添加到下拉列表中。我正在迭代versions对象的键,将每个键添加为optgroup;然后迭代每个键的数组,并将它们作为选项添加到optgroup中 渲染时,每个数组中的第一个选项将丢失 if(i
{"versions":{
"stable":["1.0","1.1"],
"work_in_progress":["wip"],
"branches":["branch1.0","branch1.1"]
}
通过jQuery,我试图将这些数据添加到下拉列表中。我正在迭代versions对象的键,将每个键添加为optgroup;然后迭代每个键的数组,并将它们作为选项添加到optgroup中
渲染时,每个数组中的第一个选项将丢失
if(items) {
$.each(items, function(key, value) {
console.log(key);
var optGroup = $("<optgroup/>");
optGroup.attr('label', key);
$.each(value, function(index, item) {
console.log("--"+item);
optGroup.append($("<option/>").val(item).text(item));
});
selObj.append(optGroup);
});
}
但是在下拉列表中,稳定的optgroup有1.1;work_in_progress optgroup没有任何内容,Branchs optgroup只有branch1.1
为什么会发生这种事 对我来说很好:谢谢巴尔马的小提琴。很抱歉浪费了你的时间。像个十足的傻瓜一样,我在select上设置了一个临时CSS来隐藏第一个选项。
"stable"
--"1.0"
--"1.1"
"work_in_progress"
--"wip"
"branches"
--"branch1.0"
--"branch1.1"