开发者

Creating rows of a table in a for loop in jsp

开发者 https://www.devze.com 2023-02-24 01:06 出处:网络
In a jsp I have a table whose rows I am creating in a loop like this, <table> <tr> <th>Item</th>

In a jsp I have a table whose rows I am creating in a loop like this,

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

Suppose 5 such rows are added, every row wil开发者_如何学运维l have the id=itemPrice, but I want the rows to have:

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

How do I do this? Please help..


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

Note what happened with the "id". It's just the counter in the for sentence.


I'd say this should work (not tested):

Replace id="itemPrice" with id="itemPrice${i+1}"

This way, you're inserting value of i inside of your html.

If this doesn't work, try id="itemPrice<%=i+1%>".

0

精彩评论

暂无评论...
验证码 换一张
取 消