2016-10-20 38 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