gpt4 book ai didi

java - 如何动态更改 HTML 输入名称

转载 作者:行者123 更新时间:2023-11-30 06:53:07 25 4
gpt4 key购买 nike

我正在创建一个小型购物车项目,其中我从 servlet 获取链接列表中的产品列表。现在,我正在表中打印链接列表的值,并提供一个选项供用户选择他们想要的数量。无论选择了数量,都应作为属性进入下一页

Here are few issues I am facing:

1: Since the list has more than 1 items, each item listed should have input field. How to I dynamically change the input name for the quantity which I can later use as an attribute.

2: The available quantity of items vary from product to product, how to I set the max value of the quantity to the available stock.

3: If I can manage to get the values, how do I set all the attributes. Is it in the for loop or outside the for loop?

这里是引用代码。 Picture of what the page looks like

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%-- <%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %> --%>
<%@page import="shoppingcart.model.items.*,java.util.*" %>

<%List<ItemDetailsPojo> listp = (List<ItemDetailsPojo>) session.getAttribute("ItemsData");

%>
<%-- <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> --%>
<Center>
<h3 style="color: blue">Welcome To the World of Shopping</h3>
</Center>
<div align='right'>Logged in as:</div>

<div align="center">
<table border="10" cellpadding="5">
<caption>
<h2>List of Items</h2>
</caption>
<tr>
<th>ItemId</th>
<th>Name</th>
<th>Category</th>
<th>Price</th>
<th>Available</th>
<th>Quantity</th>
</tr>
<%for(int i=0;i<listp.size();i++){%>
<tr>
<td><%out.println(listp.get(i).getItemId());%></td>
<td><%out.println(listp.get(i).getItemName());%></td>
<td><%out.println(listp.get(i).getCategory());%></td>
<td><%out.println(listp.get(i).getPrice());%></td>
<td><%out.println(listp.get(i).getQuantity());%></td>
<% int number = listp.get(i).getQuantity(); %>
<td><input type="number" name="should dynamically change according to the size of the list" min="0" max="should change according the the quantity available"></td>
</tr>
<%} %>
</table>

</div>

</body>
</html>

最佳答案

试试这个:

<td><input type="number" name="quantity<%=i%>" min="0" max="<%=listp.get(i).getQuantity()%>"></td>

并且您需要在循环之外设置所有属性。

关于java - 如何动态更改 HTML 输入名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42359668/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com