如何为<html:option> </html:option>创建一个循环

时间:2010-02-16 08:47:22

标签: struts

这是我的代码

<html:select property="singleSelect">
<html:option value="0">Select One </html:option> 
<script type="text/javascript">
var nfhEvents =new Array();
<% ArrayList nfhList = simple.Reject();
String[] stringArray = (String[])nfhList.toArray(new String[nfhList.size()]);
System.out.println("Size   "+stringArray.length);%>
var state = new Array(<%= stringArray %> );
for (int i = 0; (i < <%= stringArray.length%>); i++) {
    document.write("<option value='"+state[i]+"'>" + <%= stringArray[i]%>+"test" +"</option>");
}
</script>
   this is not working fine. how to print values one by one in the option. pls help asap
<html:select>

2 个答案:

答案 0 :(得分:3)

以下是我使用日历月(在javascript中)所做的一个示例

<select name="birthMonth"><option value="-1">-&nbsp;Month:&nbsp;-</option>
    <script type="text/javascript">
    var monthStr = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November","December");

        for (var i = 0; i < 12; i++) {
            if (i != 8) {                                           
              document.write('<option value="' + (i + 1) + '">' + monthStr[i] + '</option>');
        } else {
              document.write('<option value="' + (i + 1) + '" selected>' + monthStr[i] + '</option>');
        }
       }
    </script>
</select>

我希望这会对你有所帮助。

编辑:因为你正在使用Struts

<html:select property="singleSelect">
<html:option value="0">Select One </html:option> 
<% ArrayList nfhList = simple.Reject();
String[] stringArray = (String[])nfhList.toArray(new String[nfhList.size()]);
//You MUST do this
request.setAttribute("stringArray", stringArray);
%>
<logic:iterate id="s" name="stringArray">
  <html:option value="${s}"><bean:write name="s" /></html:option>
</logic:iterate>
</html:select>

答案 1 :(得分:1)

使用JSP的taglib JSTL。然后,您可以使用<c:forEach>标记显示所有选项。尽量避免使用scriptlet。