首先在servlet里面增加方法updateItem:
protected void updateItem(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
int id =WebUtils.parseInt(req.getParameter("id"),0);
int count =WebUtils.parseInt(req.getParameter("count"),1);
Cart cart=(Cart) req.getSession().getAttribute("cart");
if (cart!=null)
{
cart.updateCount(id,count);
}
resp.sendRedirect(req.getHeader("Referer"));
}
然后给我们的jsp页面的数量改成input标签然后绑定事件:
$("input.updateCount").change(function () {
var name=$(this).parent().parent().find("td:first").text();
var count=this.value;
var id =$(this).attr("bookId");
if (confirm("你确定要修改【"+name+"】的数量为:"+count+"吗?"))
{
location.href="cartServlet?action=updateItem&id="+id+"&count="+count;
}
else
{
// defaultvaLue属性是表单项Dom对象的属性。它表示默认的vaLueH性值
this.value=this.defaultValue;
}
});
<c:forEach items="${sessionScope.cart.items}" var="book">
<tr>
<td>${book.value.name}</td>
<td><input class="updateCount" bookId="${book.value.id}" style="width: 70px" type="text" value="${book.value.count}"></td>
<td>${book.value.price}</td>
<td>${book.value.totalPrice}</td>
<td><a class="deleteItem" href="cartServlet?action=deleteItem&id=${book.value.id}">删除</a></td>
</tr>
</c:forEach>