【问题标题】:How to get checkboxes values for JSP page?如何获取 JSP 页面的复选框值?
【发布时间】:2019-07-06 06:18:18
【问题描述】:

我可以通过 id 获取 jsp 页面中项目的单个值。但我无法获得复选框值。如何通过 id 或其他任何内容获取复选框值。当我点击订单页面时,它将收集所有产品名称和产品价格

截图

代码是:

<table class="table table-hover table-striped">
            <thead>
                <tr>
                    <th>ID</th>
                    <th>Choose Product</th>
                    <th>Product Name</th>
                    <th>Product Price</th>
                    <th>Action</th>
                </tr>
            </thead>
            <tbody>
                <%
                String Host = "jdbc:mysql://localhost:3306/shopbilling";
                Connection connection = null;
                Statement statement = null;
                ResultSet rs = null;
                Class.forName("com.mysql.jdbc.Driver");
                connection = DriverManager.getConnection(Host, "root", "");
                statement = connection.createStatement();
                String query = request.getParameter("q");
                String data;
                if(query != null)
                {
                data = "select * from products_tbl where product_name like '%"+query+"%' or product_price like '%"+query+"%'";
                }
                else 
                {
                data = "select * from products_tbl";
                }
                rs = statement.executeQuery(data);
                while (rs.next()) {
                %>
                <tr>
                    <td><%=rs.getString("id")%></td>
                    <td> <input type="checkbox" /> </td>
                    <td><%=rs.getString("product_name")%></td>
                    <td><%=rs.getString("product_price")%></td>
                    <td class="text-center" width="250">
                       <a href='edit.jsp?u=<%=rs.getString("id")%>' class="btn btn-warning">Edit</a>
                        <a href='delete.jsp?d=<%=rs.getString("id")%>' class="btn btn-danger">Delete</a>
                    </td>
                </tr>

                <%
                }
                %>
            </tbody>

        </table>
       <a href='order.jsp' class="btn btn-success">Order</a>

【问题讨论】:

    标签: html jsp


    【解决方案1】:

    这里你可以使用&lt;input type="checkbox"&gt;的值标签来把数据给用户。然后你需要给所有这些复选框一个属性'name',你可以保持不变,然后你可以抓取所有选定的值作为数组。

    这是您需要在此代码中进行的更改::

    <table class="table table-hover table-striped">
                <thead>
                    <tr>
                        <th>ID</th>
                        <th>Choose Product</th>
                        <th>Product Name</th>
                        <th>Product Price</th>
                        <th>Action</th>
                    </tr>
                </thead>
                <tbody>
                    <%
                    String Host = "jdbc:mysql://localhost:3306/shopbilling";
                    Connection connection = null;
                    Statement statement = null;
                    ResultSet rs = null;
                    Class.forName("com.mysql.jdbc.Driver");
                    connection = DriverManager.getConnection(Host, "root", "");
                    statement = connection.createStatement();
                    String query = request.getParameter("q");
                    String data;
                    if(query != null)
                    {
                    data = "select * from products_tbl where product_name like '%"+query+"%' or product_price like '%"+query+"%'";
                    }
                    else 
                    {
                    data = "select * from products_tbl";
                    }
                    rs = statement.executeQuery(data);
                    while (rs.next()) {
                    %>
                    <tr>
                        <td><%=rs.getString("id")%></td>
                        <td> <input type="checkbox"  name="products" value ="<%=rs.getString("id")%>" /> </td>
                        <td><%=rs.getString("product_name")%></td>
                        <td><%=rs.getString("product_price")%></td>
                        <td class="text-center" width="250">
                           <a href='edit.jsp?u=<%=rs.getString("id")%>' class="btn btn-warning">Edit</a>
                            <a href='delete.jsp?d=<%=rs.getString("id")%>' class="btn btn-danger">Delete</a>
                        </td>
                    </tr>
    
                    <%
                    }
                    %>
                </tbody>
    
            </table>
           <a href='order.jsp' class="btn btn-success">Order</a>
    

    在收到结果时,您需要使用类似这样的东西::

    <%
    
        String products[] = request.getParameterValues("products"); 
        if (products!= null && products.length != 0) {
            out.println("You have selected: ");
            for (int i = 0; i < products.length; i++) {
                 out.println(products[i]); 
            }
        }
    %>
    

    希望这对您有所帮助! 快乐编码:)

    【讨论】:

    • 点击下单按钮时,下单页面的值如何传递?
    猜你喜欢
    • 1970-01-01
    • 2013-12-23
    • 1970-01-01
    • 1970-01-01
    • 2019-09-28
    • 2016-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多