2013-04-21 84 views
0

如何設置爲我生成的列表框jsf的單個元素?我想通過JS來設置它。通過JavaScript設置列表框的元素

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

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

你不需要'我'++在你的循環的循環結束本身將增加它。 – 2013-04-21 00:36:56

回答

0
var listBox = document.getElementById('pageForm:effectiveDateListbox'); 
var dates = [new Date(2013,01,01), new Date(2013,02,03), new Date(2013,03,03)];  
for (i = 0; i != dates.length; i++) { 
    var option = document.createElement("option"); 
    var optionText = [dates[i].getMonth() + 1, dates[i].getDate(), dates[i].getFullYear()]; 
    option.text = optionText.join("/"); 
    listBox.add(option, null); 
} 
相關問題