2010-02-16 6 views
-1

c'est mon codecomment faire une boucle pour la <html:option>

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

Répondre

2

Voici un exemple de ce que je faisais avec le mois civil (en 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> 

J'espère que cela vous aide.

EDIT: Puisque vous utilisez 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> 
+0

peut plaire à votre gestionnaire avec des scriptlets .... lol –

+0

erreur de dipalying comme ci-dessous .. Impossible de trouver bean: "stringArray" dans toute portée – Manu

+0

ok .... vérifier ma modification .... –

1

de JSTL taglib Utiliser JSP. Vous pouvez ensuite utiliser la balise <c:forEach> pour afficher toutes les options. Essayez d'éviter les scriptlets.

+0

mon manager m'a demandé d'utiliser scriplets.pls aider dès que possible .. – Manu

+0

reformater ce qui précède code..if son ayant toute erreur .. Et vous – Manu