最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - how to set values to combobox dynamically in javascript - Stack Overflow

programmeradmin4浏览0评论

this is how i set value to a bobox 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 bo box i set values to loaded bo 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

this is how i set value to a bobox 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 bo box i set values to loaded bo 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

Share Improve this question edited Sep 17, 2012 at 9:42 Java Questions asked Sep 17, 2012 at 8:57 Java QuestionsJava Questions 7,95343 gold badges119 silver badges177 bronze badges 3
  • If the var reportID =... is placed in a function, remove the var before the reportID =... – Jay Commented Sep 17, 2012 at 9:12
  • I couldn't reproduce the error using the updated info. The option has the proper '<%=reportname%>' text and '<%=reportid%>' value. By "reportID is not set", do you meant that the result after the option has been added, it has no text and no value? – Jay Commented Sep 17, 2012 at 10:04
  • those two e from session, which are already present in bobox say reportid =1 and reportname = sample anyway thanks for the interest – Java Questions Commented Sep 17, 2012 at 10:08
Add a ment  | 

2 Answers 2

Reset to default 5

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;
        }
    }
}

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

document.getElementById("reportnames").value = "" + reportID;


Original: To set the selected option of a bobox (assuming you mean html "select") you need to set the "selected" attribute of the desired option to 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:

select.options[i].ReportId == reportID


If you set the reportID as the "id"-attribute of each option you could even do it like this:

document.getElementById(reportID).selected = true;
发布评论

评论列表(0)

  1. 暂无评论