我正在运行这个脚本:
function insertformat(formats){
//Deletes the content of the dropdown if there was any
document.getElementById('format').options.length = 50;
//fills the dropdown with an associative array
for (var key in formats)
var newOption = "<option value='"+key+"'>"+formats[key]+"</option>";
$("#format").append(newOption);
}
填写以下选择:
<select id="format"></select>
但是仅存储数组的最后一个元素。在 Debug模式下,似乎每次循环完成时都会覆盖下拉列表,而不是添加新元素。
最佳答案
目前,appned
代码不在内部循环
,因此仅将最后一个值
添加到下拉列表中。像这样改变你的循环。
for (var key in formats)
{
var newOption = "<option value='"+key+"'>"+formats[key]+"</option>";
$("#format").append(newOption);
}
关于javascript - 尝试用选项填充下拉菜单。仅存储一个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37718652/