jquery mysql实现加入购物车_jQuery实现购物车全功能

本文介绍了使用jQuery创建购物车的完整功能,包括全选、全取消、商品增减、总价计算、删除商品等操作。通过示例代码展示了如何实现购物车的交互逻辑,并提供了CSS样式效果。
摘要由CSDN通过智能技术生成

本文实例为大家分享了jQuery实现购物车全功能的具体代码,供大家参考,具体内容如下

效果图:

5f4000982d84a6892304785d88f69548.gif

HTML&&CSS:

Document

* {

margin: 0;

padding: 0;

}

.tab {

width: 500px;

border-collapse: collapse;

margin: 0 auto;

}

.tab td,

th {

border: 1px solid #000;

}

.tab .num {

width: 20px;

}

.tab .add,

.sub {

width: 20px;

}

.current {

background-color: pink;

}

全选 商品名称单价数量小计操作

电脑¥200.20

-

+

¥200.20删除手机¥100.30

-

+

¥100.30删除空调¥1000.99

-

+

¥1000.99删除

已选1件商品

总计:

0

删除选中商品

清空购物车

JS:

//里面三个小的复选按钮选中状态跟着 全选按钮走

//因为checked是复选框的固有属性,此时利用prop()获取和设置该属性

$(function() {

getSum();

$(".checkAll").change(function() {

// console.log($(this).prop("checked"));//全选按钮的状态

$(".ed,.checkAll").prop("checked", $(this).prop("checked"));

getSum();

if ($(".ed,.checkAll").prop("checked")) {

//如果全选,让所有商品添加类名(背景颜色)

$(".tab tbody").children().addClass("current");

} else {

$(".tab tbody").children().removeClass("current");

}

})

//如果所有小按钮的个数都被选了,全选按钮就选上,如果小按钮没有被选上,则全选按钮就不选上

//:checked选择器,查找本选中的表单元素

$(".ed").change(function() {

// console.log($(".ed:checked").length);//小复选框选中的个数

// console.log($(".ed").length);

//console.log($(this).prop("checked"));

if ($(".ed:checked").length === $(".ed").length) {

$(".checkAll").prop("checked", true);

} else {

$(".checkAll").prop("checked", false);

}

getSum();

if ($(this).prop("checked")) {

$(this).parents("tr").addClass("current");

} else {

$(this).parents("tr").removeClass("current");

}

})

$(".add").click(function() {

let n = parseInt($(this).siblings(".num").val());

//console.log(n);

n++;

$(this).siblings(".num").val(n);

let price = $(this).parent().siblings(".price").html();

price = price.substr(1);

//console.log(price);

$(this).parent().siblings(".small_total").text("¥" + (n * price).toFixed(2));

getSum();

})

$(".sub").click(function() {

let n = parseInt($(this).siblings(".num").val());

//console.log(n);

if (n === 1) {

return false;

}

n--;

$(this).siblings(".num").val(n);

let price = $(this).parent().siblings(".price").html();

price = price.substr(1);

//console.log(price);

$(this).parent().siblings(".small_total").text("¥" + (n * price).toFixed(2));

getSum();

})

//用户也可以直接修改表单num里面的值(小bug),同样计算小计

$(".num").change(function() {

let n = $(this).val();

let price = $(this).parent().siblings(".price").html();

price = price.substr(1);

$(this).parent().siblings(".small_total").text("¥" + (n * price).toFixed(2));

getSum();

})

function getSum() {

let count = 0; //计算总件数

let money = 0; //计算总价钱

$(".num").each(function(index) {

if ($(".ed").eq(index).prop("checked") == true) {

count += parseInt($(".num").eq(index).val());

money += parseFloat($(".small_total").eq(index).text().substr(1));

}

})

$(".num_sum").html(count);

$(".sum").html(money.toFixed(2));

}

//删除商品模块

//点击删除之后一定是删除当前的商品,所以从$(this)出发

$(".delete").click(function() {

//删除的是当前的商品

$(this).parent().remove();

$(".ed").change();

getSum();

clearCheckAll();

})

//删除选定的商品:小的复选框如果选中就删除对应的商品

$(".delSome").click(function() {

//删除的是选中的商品

$(".ed:checked").parent().parent().remove();

getSum();

clearCheckAll();

})

//清空购物车

$(".delAll").click(function() {

$(".tab tbody").empty();

getSum();

clearCheckAll();

})

function clearCheckAll() {

if ($(".tab tbody")[0].innerText == '') {

$(".checkAll").prop("checked", false);

}

}

})

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值