这就是我使用 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);
}
加载组合框后,我将值设置为这样加载的组合,
document.getElementById("reportnames").value=reportID;
但是没有设置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 = "";
}
使用上面的方法它没有给我异常但没有结果。
问候
最佳答案
我没有删除该值,而是添加了以下代码,它解决了我的问题。
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;
}
}
}
关于javascript - 如何在javascript中动态设置组合框的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12455981/