2016-10-20 36 views
0

我有一個表格,代表客戶的購物車中有「從購物車中刪除」按鈕的所有產品。它使用戶能夠移除購物車中的某個產品。以下是代碼:爲什麼我的桌子這樣表現?

<form 
     action="${pageContext.request.contextPath}/customer/removeProduct" 
     method="post"> 
     <input type="hidden" name="page" value="${page}"> 
     <table class="table"> 
      <thead> 
       <tr> 
        <th>Name</th> 
        <th>Quantity</th> 
        <th>Amount</th> 
       </tr> 
      </thead> 
      <tbody> 
       <c:forEach items="${productsInCart}" var="product" 
        varStatus="status"> 
        <input type="hidden" class="form-control" name="upc" 
          value="${product.getProduct().getUpc()}"> 
        <tr class="warning"> 

         <td>${product.getProduct().getName()}</td> 
         <td>${product.getQuantity()}</td> 
         <td style="color: green;">&#8369; ${product.totalPrice()}</td> 
         <td><input type="submit" class="btn btn-warning btn-xs" 
          value="Remove from cart"></td> 
        </tr> 
       </c:forEach> 
      </tbody> 
     </table> 
    </form> 

通過在控制器中發送其UPC(通用產品代碼)來刪除產品。但是,當點擊「從購物車中移除」按鈕時,購物車中所有產品的UPC都會被髮送。我不知道爲什麼會發生這種情況。

+0

哪裏是「刪除購物車」按鈕的代碼?如果按鈕提交表單,它將包含您的$ {productsInCart}' – bhantol

+0

中的所有'input:upc'字段在最後,​​我的朋友。​​ saluyotamazing

+0

明白了。它將提交所有UPC的表單,因此代碼的行爲應該與它應該一樣。看起來像你想嘗試刪除只調用刪除的產品。 – bhantol

回答

0

基於對我的評論的假設,你可以這樣做:

<table class="table"> 
    <thead> 
     <tr> 
      <th>Name</th> 
      <th>Quantity</th> 
      <th>Amount</th> 
     </tr> 
    </thead> 
    <tbody> 
     <c:forEach items="${productsInCart}" var="product" 
      varStatus="status"> 
      <form action="${pageContext.request.contextPath}/customer/removeProduct" method="post"> 
       <input type="hidden" name="page" value="${page}"> 
       <input type="hidden" class="form-control" name="upc" 
         value="${product.getProduct().getUpc()}"> 
       <tr class="warning"> 

        <td>${product.getProduct().getName()}</td> 
        <td>${product.getQuantity()}</td> 
        <td style="color: green;">&#8369; ${product.totalPrice()}</td> 
        <td><input type="submit" class="btn btn-warning btn-xs" 
         value="Remove from cart"></td> 
       </tr> 
      </form> 
     </c:forEach> 
    </tbody> 
</table> 
+0

我應該刪除

標籤嗎? – saluyotamazing

+0

是的。將表單的範圍移動到每個表單的內部 – bhantol