如何在javascript中动态设置组合框的值

Jav*_*ons 3 html javascript combobox

这就是我使用 dwr 调用为组合框设置值的方法,

var reportID = '<%=reportid%>'; var reportName = '<%=reportname%>'; loadReportNames(reportUserID);

function loadReportNames(reportUserID){
    CustomiseReportAction.getReportNames(reportUserID, addReportNamesDropDown);
}
function addReportNamesDropDown(resultMap){
    dwr.util.removeAllOptions("reportnames");
    dwr.util.addOptions("reportnames",resultMap);
}
Run Code Online (Sandbox Code Playgroud)

加载组合框后,我将值设置为加载的组合,如下所示,

document.getElementById("reportnames").value=reportID;
Run Code Online (Sandbox Code Playgroud)

但reportID没有设置,

可能是什么问题请帮我解决这个问题。

UPDATE :

function addCombo() {
    var reportID = '<%=reportid%>';
    var reportName = '<%=reportname%>';
    var textb = document.getElementById("reportnames");

    var option = document.createElement("option");
    option.text = reportName;
    option.value = reportID;
    option.selected="selected";
    try {
        textb.add(option, null); //Standard
    }catch(error) {
        textb.add(option); // IE only
    }
    textb.value = "";
}
Run Code Online (Sandbox Code Playgroud)

使用上面的方法它没有给我例外,但没有结果。

问候

Jav*_*ons 5

我没有删除该值,而是添加了以下代码,它解决了我的问题。

function addCombo() {
    var reportID = '<%=reportid%>';
    var options= document.getElementById('reportnames').options;
    for (var i= 0, n= options.length; i < n ; i++) {
        if (options[i].value==reportID) {
            document.getElementById("reportnames").selectedIndex = i;
            break;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)