添加选项以选择IE中的下拉列表

zor*_*t15 3 html javascript

我正在尝试在运行时向选择下拉列表添加项目.到目前为止,它在Firefox和Opera中工作,但它似乎不适用于IE7或8.

应该发生的事情是,当用户选择一个中心时,人员下降就会被中心的人员填满....

//Clear out the all of the exisiting items
if (document.getElementById("ddlPersonnel").hasChildNodes) {
    while (document.getElementById("ddlPersonnel").childNodes.length > 0) {
        document.getElementById("ddlPersonnel").removeChild(document.getElementById("ddlPersonnel").firstChild);
    }
}

//Add the "Select Personnel" option
var FirstOpt = document.createElement('OPTION');
FirstOpt.value = "";
FirstOpt.innerText = "Select Personnel";
alert("blah1");
document.getElementById("ddlPersonnel").options.add(FirstOpt, null);    //It dies here with a "Type Mismatch" error
alert("blah2");
Run Code Online (Sandbox Code Playgroud)

它在两个警报之间的行上死亡,出现"类型不匹配"错误.

Que*_*tin 6

使用新选项而不是createElement.

var sel = document.getElementById("ddlPersonnel");
var opt = sel.options;
opt[opt.length] = new Option("Label","Value")
Run Code Online (Sandbox Code Playgroud)

(那应该有用,但我没有测试过)

  • 这对我不起作用.我收到一个错误说 - "对象不支持此属性".我正在使用IE8 (2认同)