簡體   English   中英

如何將帶有選中復選框的表行值從jsp獲取到servlet

[英]how to get table row values with the selected checkbox to servlet from jsp

這是我的代碼。

        <form action="order" method="post">
    <table border="1" cellpadding="15" cellspacing="0">
            <tr>
                <th>Product</th>
                <th>rate</th>
                <th>Quantity</th>
                <th>Manufacture Name</th>
                <th>Select Items</th>
            </tr>
            <c:forEach var="entry" items="${productModel.entries}">
                <tr>
                    <td>${entry.pname }</td>
                    <td>${entry.rate }</td>
                    <td>
                        <select name = "quantity" style="width: 90px; ">
                                <c:set var="total" value="${entry.currentstock}" />
                            <c:forEach begin="0" end="${total-1}" step="1" varStatus="loop">
                                <option>${loop.count}</option>
                            </c:forEach>
                        </select>
                    </td>
                    <td>${entry.manu_name }</td>
                    <td><input type="checkbox"></td>
                </tr>
            </c:forEach>
        </table>
        <input type="submit" name="order" value="Confirm Order">
        </form>

我想獲取選中的復選框,然后將這些值添加到servlet。 無法找到方法。請引導我

問題出在您的html中:

<option>${loop.count}</option>

它應該是

<option value="${loop.count}">${loop.count}</option>

所選option標簽的value屬性將被提交到服務器(servlet)。
這是選擇選項標簽組合的默認行為:

<select name="select1">
    <option value="value1">label1</option>
    <option value="value2">label2</option>
</select>

編輯:

為了提交其他值,僅顯示它們是不夠的

<td>${entry.pname }</td>

您需要添加input等表單字段元素

<td>${entry.pname}<input type="hidden" name="name" value="${entry.pname}"/></td>

如果要使字段可編輯,則type可以是text

<td><input type="text" name="name" value="${entry.pname}"/></td>

對於所有其他表行也應這樣做。

另外,我建議每行都是一種形式,而不是只有一種形式。 而不是復選框,您可以具有“提交”按鈕。

<table border="1" cellpadding="15" cellspacing="0">
        <tr>
            <th>Product</th>
            <th>rate</th>
            <th>Quantity</th>
            <th>Manufacture Name</th>
            <th>Select Items</th>
        </tr>
        <c:forEach var="entry" items="${productModel.entries}">
            <tr>
              <form action="order" method="post">
                ...
                <td><input type="submit" name="order" value="Confirm Order"></td>
              </form>
            </tr>
        </c:forEach>
    </table>

    </form>

上述解決方案用於提交單行。

但是您希望用戶能夠一次訂購許多產品。
因此,更好的方法是僅使用在表單生成時就知道的產品ID。 表單/行包含帶產品ID的隱藏輸入和帶數量的選擇。
提交后,您可以從數據庫中檢索相同的產品並可以訪問詳細信息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM