有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

我正在创建一个小型购物车项目,在这个项目中,我从servlet中获得一个链接列表中的产品列表。现在,我在一个表中打印linkedlist的值,其中有一个选项供用户选择他们想要的数量。 选择的数量应作为属性转到下一页

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> 

共 (2) 个答案

  1. # 1 楼答案

    像这样编辑代码

    <%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>
                        <%
         String name;
        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();
        name="quantity".concat(listp.get(i).getQuantity());
         %>
                                <td><input type="number"name="<%=name%>" min="0" max="should change according the the quantity  available"></td>
                            </tr>
                        <%} %>
                    </table>
    
                </div>
    
    
        </body>
        </html>
    
  2. # 2 楼答案

    试试这个:

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

    并且需要设置循环外部的所有属性