Javascript 如何在javascript中动态设置组合框的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12455981/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to set values to combobox dynamically in javascript
提问by Java Questions
this is how i set value to a combobox using dwr call,
这就是我使用 dwr 调用为组合框设置值的方式,
var reportID = '<%=reportid%>';
var reportName = '<%=reportname%>';
loadReportNames(reportUserID);
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,
但是没有设置reportID,
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
问候
回答by Wutz
Edit: I just double checked and it should work like you did it, so disregard my original post. Are you sure the content of reportID exactly matches one of the options? If its a number, not a string, you might want to try
编辑:我只是仔细检查了一下,它应该像你一样工作,所以请忽略我原来的帖子。您确定 reportID 的内容与选项之一完全匹配吗?如果它是一个数字,而不是一个字符串,你可能想尝试
document.getElementById("reportnames").value = "" + reportID;
Original:
To set the selected option of a combobox (assuming you mean html "select") you need to set the "selected" attribute of the desired option to true.
原文:要设置组合框的选定选项(假设您的意思是 html“select”),您需要将所需选项的“selected”属性设置为 true。
var select = document.getElementById("reportnames");
for (var i = 0; i < select.options.length; i++)
{
if (...)
select.options[i].selected = true;
}
You will need some way to identify the option, I'd do it by saving the reportID in it. Then you could replace the ... with:
您将需要某种方式来识别该选项,我会通过在其中保存 reportID 来实现。然后你可以将 ... 替换为:
select.options[i].ReportId == reportID
If you set the reportID as the "id"-attribute of each option you could even do it like this:
如果您将 reportID 设置为每个选项的“id”属性,您甚至可以这样做:
document.getElementById(reportID).selected = true;
回答by Java Questions
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;
}
}
}