Creating table rows in a for loop in jsp

In jsp, I have a table whose rows I create in such a loop,

<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 lines are added, each line will have id = itemPrice, but I want the lines to have:

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

How can I do it? Please, help..

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

Notice what happened with "id". This is just a counter in the sentence.

+5
source

I would say that this should work (not verified):

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

This way you insert the value of i inside your html.

If this does not work, try id="itemPrice<%=i+1%>" .

0
source

Source: https://habr.com/ru/post/1347859/


All Articles