在jsp的for循环中创建表的行

问题描述

| 在一个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..
我该怎么做呢?请帮忙..     

解决方法

<input type=\"text\" id=\"itemPrice<%=i%>\" maxlength=\"5\" size=\"5\" style=\"border:none;\" value=\"$<%= cartItem[3]%>\" />
注意“ id”发生了什么。它只是for语句中的计数器。     ,我说这应该有效(未经测试): 将
id=\"itemPrice\"
替换为
id=\"itemPrice${i+1}\"
这样,您将在html中插入value5ѭ的值。 如果这不起作用,请尝试
id=\"itemPrice<%=i+1%>\"
。