<html>
<head>
<script type="text/javascript">
function delItem(id) {
var buyNum = $("#buyNum"+id).val();
var salePrice = $("#salePrice"+id).val();
if(buyNum == 1){
alert("选购的商品不能小于1件");
return;
}
var url = "${path}/or/shopCart/update";
var params = {"id" : id, "buyNum" : (buyNum-1)};
$.post(url,params,function(data){
if (data == "true") {
if ($("#checkbox" + id).attr("checked")) {
$("#totalPrice").text($("#totalPrice") - salePrice);
}
$("#sumPrice"+id).text($("#sumPrice"+id) - salePrice);
$("#buyNum"+id).val(buyNum - 1);
}
},"json");
}
function addItem(id, limit) {
var buyNum = $("#buyNum"+id).val();
var salePrice = $("#salePrice"+id).val();
//检测库存
if((buyNum*1+1) > limit){
alert("该商品的库存为:" + limit + ",无法再增加购买量");
return;
}
var url = "${path}/or/shopCart/update";
var params = {"id" : id, "buyNum" : (buyNum*1 + 1)};
$.post(url, params, function(data){
if (data == "true") {
if ($("#checkbox" + id).attr("checked")) {
$("#totalNum").text($("#totalNum")*1 + 1);
$("#totalPrice").text($("#totalPrice")*1 + salePrice);
}
$("#sumPrice"+id).text($("#sumPrice"+id)*1 + salePrice);
$("#buyNum"+id).val(buyNum + 1);
}
},"json");
}
function remove(id) {
//询问框
var url = "${path}/or/shopCart/delete";
var params = {"id" : id};
$.post(url, params, function(data){
$.lw.layer.success();
setTimeout("location.reload();",2000);
},"json");
}
function checkedOpt() {
var arr = getChecked();
var totalPrice = 0;
var totalNum = 0;
for (var i = 0, arr.length > 0, i++) {
totalPrice = totalPrice*1 + $("#sumPrice" + arr[i]).val();
totalNum = totalNum*1 + $("#buyNum" + arr[i]).val();
}
$("#totalNum").text(totalNum);
$("#totalPrice").text(totalPrice);
}
//全选
function checkedAll() {
$("input[type='checkbox']").attr("checked",'true');//全选
}
//全不选
function notChecked() {
$("input[type='checkbox']").removeAttr("checked");//取消全选
}
//结算,利用表单提交
function account() {
var arr = getChecked();
}
function getChecked() {
var arr = new Array();
$("input[type='checkbox']:checked").each(function(index, element){
arr.push(element.value);
});
return arr;
}
</script>
</head>
<body>
<form action="">
<div>购物车页面</div>
总的数量:${cartVO.totalNum}
<c:forEach items="${cartVO.shopCarts}" var="cart">
<input type="checkbox" id="checkbox${cart.id}" value="${cart.id}" name="ids" checked="checked"/>
<div >
名称:${cart.stockConf.goodsName }
规格:<c:forEach items="${cart.stockConf.goodsSpecificationVals}" var="gsv">
${gsv.name} : ${gsv.value}
</c:forEach>
购买数量:
<div οnclick="delItem('${cart.id}')"> - </div>
<div id="buyNum${cart.id}">${cart.buyNum}</div>
<div οnclick="addItem('${cart.id}', '${cart.stockConf.currStock}')"> + </div>
单价:
<div id="salePrice${cart.id}">${cart.stockConf.salePriceStr }</div>
小计
<div id="sumPrice${cart.id}">${cart.sumSalePriceStr}</div>
<a href="javascript:void(0);" οnclick="remove('${cart.id}')"> 删除 </a>
</div>
</c:forEach>
选中数量:<div id="totalNum">${cartVO.totalNum}</div>
选中价格:<div id="totalPrice">${cartVO.totalPriceStr }</div>
</form>
<a href="javascript:void(0);" οnclick="account()">结算</a>
</body>
</html>
购物车基本的增,删,减,全选,全不选
最新推荐文章于 2022-11-02 09:31:25 发布