商城案例Ajax修改购物车单品数量

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sinat_27108161/article/details/53558649

Jsp端:

//2.请求地址为:bookServlet;

var url = "bookServlet";
//3.请求参数为:
var idVal = $.trim(this.name);
var args = {"method":"updateItemQuantity", "id":idVal, "quantity":quantityVal, "time":new Date()};
//更新当前页面的 bookNumber 和 TotalMoney
$.post(url, args, function(date){
var bookNumber = date.bookNumber;
var totalMoney = date.totalMoney;

alert(bookNumber);

$("#bookNumber").text("您的购物车中有"+bookNumber+"本书.");
$("#totalMoney").text("总金额  ¥ "+totalMoney);
},"JSON");

});

服务器端:

Map<String, Object> result = new HashMap<String, Object>();
result.put("bookNumber", sc.getBookNumber());
result.put("totalMoney", sc.getTotalMoney());

Gson gson = new Gson();
String json = gson.toJson(result);
response.setContentType("text/javascript");
response.getWriter().print(json);


改变单品数量时若为0,触发删除操作的方法:

为<a>,标签添加onclick属性,直接调onclick方法即可

$("a").each(function(){
this.onclick = function(){
var serializeVal = $(":hidden").serialize();
var href = this.href + "&" + serializeVal;
alert(href);
window.location.href = href;
return false;
};

});


if(flag2){
$a = $tr.find("td:last").find("a");
$a[0].onclick();
return;
}

猜你喜欢

转载自blog.csdn.net/sinat_27108161/article/details/53558649