如何在javascript中动态设置值到combobox [英] how to set values to combobox dynamically in javascript
问题描述
这是我如何使用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);
}
加载组合框后, p>
document.getElementById(reportnames)。value = reportID;
但未设置reportID,
可能的问题请帮我解决这个问题。
更新:
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 =;
}
使用上面的方法它给了我没有例外,但没有结果。
$ b
我没有删除该值,而是添加了以下代码,它解决了我的问题。
function addCombo(){
var reportID ='<%= reportid%> ;';
var options = document.getElementById('reportnames')。options;
for(var i = 0,n = options.length; i if(options [i] .value == reportID){
document.getElementById reportnames)。selectedIndex = i;
break;
}
}
}
this is how i set value to a combobox using dwr call,
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);
}
after loading the combo box i set values to loaded combo like this,
document.getElementById("reportnames").value=reportID;
but the reportID is not set,
what could be the problem please help me to resolve this.
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 = "";
}
used above method it gives me no exception but no results.
Regards
I have not removed the value rather i added the following code, it solved me the problem.
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中动态设置值到combobox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!