在JSP

2011-04-13 35 views
4

創建一個表的行中的for循環在JSP我有一個表,它的行我創造了這樣一個循環,在JSP

<table> 
<tr> 
<th>Item</th> 
<th>Quantity</th> 
<th>Price</th> 
<th>Total</th> 
<th>Actions</th> 
</tr> 
<tr> 
<% 
String[] cartItems = (String[]) request.getSession().getAttribute("cartList"); 
for (int i = 0; i < cartItems.length; i++) { 
%> 
    <td> 
     <input type="text" id="itemPrice" maxlength="5" size="5" style="border:none;" value="$<%= cartItem[3]%>" /> 
    </td> 
<% } %> 
</tr> 
</table> 

設5個這樣的行補充說,每一行都會有該id = itemPrice,但我想要的行有:

id=itemPrice1 
id=itemPrice2.. and so on.. 

我該如何做到這一點?請幫忙..

回答

5
<input type="text" id="itemPrice<%=i%>" maxlength="5" size="5" style="border:none;" value="$<%= cartItem[3]%>" /> 

請注意「id」發生了什麼。這只是句子中的計數器。

+0

當然!爲什麼我沒有想到這一點!謝謝。 – sherry 2011-04-13 07:42:23

0

我想說這應該工作(未測試):

更換id="itemPrice"id="itemPrice${i+1}"

這樣一來,你將你的HTML裏面的i值。如果這不起作用,請嘗試id="itemPrice<%=i+1%>"