我通过foreach显示每个商品详情,该怎么显示总额并且把它传给action?)
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Map<String,Integer> shopcar = (Map<String,Integer>)request.getSession().getAttribute("shopcar");
Set<String> isbns = shopcar.keySet();
for(String isbn : isbns){
String value = request.getParameter(isbn);
shopcar.put(isbn, Integer.parseInt(value)); //更新购物车中的购买数量
}
UserBiz biz = new UserBiz();
try {
List<TBook> books = null;
if(shopcar.keySet().size()>0){
books = biz.getShopcarData(shopcar.keySet());
}
double allPrice = 0;
for(TBook bk : books){
int buyCount = shopcar.get(bk.getIsbn());
bk.setBuyCount(buyCount); //设置图书数量
allPrice = allPrice + bk.getPrice()*buyCount;
}
request.setAttribute("allPrice", allPrice);
request.setAttribute("books", books);
request.getRequestDispatcher("/main/checkout.jsp").forward(request, response);
} catch (Exception e) {
Log.logger.error(e.getMessage(),e);
request.setAttribute("msg", "网络异常,请和管理员联系");
request.getRequestDispatcher("/err/error.jsp").forward(request,response);
}
}
}
可以在foreach后通过发送ajax请求给后台的action
$.post("money/totalMoney.action",{totalmoney:totalmoney},function(result){
$("span").html(result);
});
点击一下 获取DOM对应的单价数量 +1 再插入到原来位置,,
然后获取(单价*数量) 获取的总额插入到你想要的位置 就显示出来了
再用AJAX传递