我使用下面的代码添加下拉列表选项
//HTML CODE
select(name='folist', id='folist').
//JS Code
for(var i=0;i<data.foNameArray.length;i++){
var combo = document.getElementById("folist");
option = document.createElement("option");
option.text = data.foNameArray[i];
option.value =data.foIdArray[i];
try {
combo.add(option, null); //Standard
}catch(error) {
combo.add(option); // IE only
}
}
它完美有效。现在我怀疑如何在添加like selected =“selected”时选择值。
答案 0 :(得分:2)
您可以像这样设置selected property选项,
option.selected = true;
答案 1 :(得分:2)
您可以使用setAttribute
来执行此操作
option = document.createElement("option");
option.setAttribute("selected","selected");