I highly recommend you use a format like the following:
var options =
[
{
"text" : "Option 1",
"value" : "Value 1"
},
{
"text" : "Option 2",
"value" : "Value 2",
"selected" : true
},
{
"text" : "Option 3",
"value" : "Value 3"
}
];
var selectBox = document.getElementById('rec_mode');
for(var i = 0, l = options.length; i < l; i++){
var option = options[i];
selectBox.options.add( new Option(option.text, option.value, option.selected) );
}
You don't run in to the problem of having to select a default option and you can easily generate the options array dynamically.
-- UPDATE --
ES6 version of the above code:
let optionList = document.getElementById('rec_mode').options;
let options = [
{
text: 'Option 1',
value: 'Value 1'
},
{
text: 'Option 2',
value: 'Value 2',
selected: true
},
{
text: 'Option 3',
value: 'Value 3'
}
];
options.forEach(option =>
optionList.add(
new Option(option.text, option.value, option.selected)
)
);
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…