-1

我想通过在 javascript 代码中设置它们来直接设置在 jsf 块中定义的列表框元素,但是 document.getElementById() 返回 null ?

JSF 代码:

<h:panelGroup id="dtEffectiveDate">
                                <h:outputText value="EffectiveDates:" style="margin-left: 250px;width:76px;"/>
                                    <h:selectManyListbox id="effectiveDateListbox" style="margin-left: 15px; width:76px;" size="2">
                                        <c:selectItems id="effectiveDateSelectItems" value="#{manualDataEntryRegText.effectiveDates}"/>
                                    </h:selectManyListbox>
                                </h:panelGroup> 

JavaScript 代码:

CalendarPopup.handleSelectForMultipleDates = function(type, args, obj, inputEl)
    {
    alert("MultiSelect");
    var arrDates = CalendarPopup.effectiveDateCalendar.getSelectedDates();
    var length = arrDates.length;
    var i=0;
    var tokenizedDates = "";

    var txtDate = document.getElementById("pageForm:effectiveDateListbox");
    alert(txtDate); // returns null
    for (i = 0; i!= length; i++) 
    {
        var date = arrDates[i];
        var displayMonth = date.getMonth() + 1;
        var displayYear = date.getFullYear();
        var displayDate = date.getDate();
        tokenizedDates = displayMonth.toString(10) + "/" + displayDate.toString(10) + "/" + displayYear.toString(10);
        effectiveDateListbox.options[i].value = tokenizedDates;
        i++;
    } 


    }

编辑:我能够获取列表框但无法设置它:如何设置列表框?

var listBox = document.getElementById('pageForm:effectiveDateListbox');
        for (i = 0; i!= length; i++) 
        {
            var date = arrDates[i];
            var displayMonth = date.getMonth() + 1;
            var displayYear = date.getFullYear();
            var displayDate = date.getDate();
            tokenizedDates = displayMonth.toString(10) + "/" + displayDate.toString(10) + "/" + displayYear.toString(10);
            listBox.options[i].value = tokenizedDates;
            i++;
        }
4

1 回答 1

0

这不应该返回null

<select id="pageForm:effectiveDateListbox" name="pageForm:effectiveDateListbox" multiple="multiple" size="0"></select>

document.getElementById('pageForm:effectiveDateListbox');

如果是,则可能是select在您的 javascript 运行时该元素不存在。为了防止这种情况,您可以尝试:

window.onload = function(){
    // do stuff here
};
于 2013-04-19T04:45:06.043 回答