[英]How to provide unique id to a button in a JSP loop
I would like to give each button in the loop a unique ID to associate the button id to that particular row. 我想给循环中的每个按钮一个唯一的ID,以将按钮ID关联到该特定行。
Any help in this matter would be highly appreciated! 在这方面的任何帮助将不胜感激!
while(resultSet.next()){
%>
<tr bgcolor="#DEB887">
<td><%=resultSet.getString("productid") %></td>
<td><%=resultSet.getString("productname") %></td>
<td><%=resultSet.getInt("price") %></td>
<td><%=resultSet.getString("quantity") %></td>
<td><%=resultSet.getString("totalprice") %></td>
<td><button id= type="button" onclick="addToCart()">Add to Cart</button></td>
</tr>
<%
You are doing your own looping... You could use an int variable and use it to increment for each iteration ... see the example below; 您正在执行自己的循环...您可以使用int变量,并使用它在每次迭代中递增...参见下面的示例;
<%
int idCounter=1;
while(resultSet.next()){
%>
<tr bgcolor="#DEB887">
<td><%=resultSet.getString("productid") %></td>
<td><%=resultSet.getString("productname") %></td>
<td><%=resultSet.getInt("price") %></td>
<td><%=resultSet.getString("quantity") %></td>
<td><%=resultSet.getString("totalprice") %></td>
<td><button id="addToCartButtonId<%=idCounter%>" type="button" onclick="addToCart()">Add to Cart</button></td>
</tr>
<%
idCounter++;
%>
For each row, the button will have a unique id. 对于每一行,按钮将具有唯一的ID。 Hope this helps ...
希望这可以帮助 ...
Thanks. 谢谢。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.