1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > 购物车结算页面案例jQuery

购物车结算页面案例jQuery

时间:2024-06-24 11:19:30

相关推荐

购物车结算页面案例jQuery

购物车结算页面案例jQuery

$(function() {//1.全选 全不选//全选按钮checkall 状态赋值给小按钮j-checkboxs// 事件使用change$(".checkall").change(function() {$(this).prop("checked");$(".j-checkbox,.checkall").prop("checked", $(this).prop("checked"));if ($(this).prop("checked")) {//让所有商品添加类名$(".cart-item").addClass("check-cart-item");} else {$(".cart-item").removeClass("check-cart-item");}});// 如果小按钮被选定的数量没有3个就不勾选全选,如果有3个则全选按钮选上$(".j-checkbox").change(function() {// 2. 判断被选中的小按钮个数 = 3 ?$(".j-checkbox:checked").length表示被选中的小按钮个数//$(".j-checkbox").length表示所有的小按钮个数if ($(".j-checkbox:checked").length === $(".j-checkbox").length) {$(".checkall").prop("checked", true);} else {$(".checkall").prop("checked", false);}if ($(this).prop("checked")) {//让当前商品添加类名$(this).parents(".cart-item").addClass("check-cart-item");} else {$(this).parents(".cart-item").removeClass("check-cart-item");}})// 3. 增减商品数量模块 首先声明一个变量,当我们点击+号(increment),就让这个值++,然后赋值给文本框。$(".increment").click(function() {//得到当前兄弟文本框的值var n = $(this).siblings(".itxt").val();console.log(n);n++;$(this).siblings(".itxt").val(n);// 计算小计模块 根据文本框的值 乘以 当前商品的价格 就是 商品的小计// 当前商品的价格 p 从this出发,this的爸爸的爸爸的兄弟代表价格var p = $(this).parents(".p-num").siblings(".p-price").html();p = p.substr(1);// console.log(p);toFixed(n)表示保留n位小数$(this).parent().parent().siblings(".p-sum").html("¥" + (p * n).toFixed(2));getSum();});$(".decrement").click(function() {//得到当前兄弟文本框的值var n = $(this).siblings(".itxt").val();console.log(n);if (n == 1) {return false; //程序遇到return 不再执行后面内容}n--;$(this).siblings(".itxt").val(n);//计算小件总价格var p = $(this).parents(".p-num").siblings(".p-price").html();p = p.substr(1);// console.log(p); toFixed(n)表示保留n位小数$(this).parent().parent().siblings(".p-sum").html("¥" + (p * n).toFixed(2));getSum();});//4.计算修改文本框的值$(".itxt").change(function() {// 先得到文本框的值 再乘上价格var n = $(this).val();var p = $(this).parents(".p-num").siblings(".p-price").html();p = p.substr(1);// console.log(p); toFixed(n)表示保留n位小数$(this).parent().parent().siblings(".p-sum").html("¥" + (p * n).toFixed(2));getSum();});// 文本框里面的值需要用each遍历// 5.getSum();function getSum() {var count = 0; //总件数var money = 0; //总价格$(".itxt").each(function(i, ele) {count += parseInt($(ele).val());})$(".amount-sum em").text(count);$(".p-sum").each(function(i, ele) {money += parseFloat($(ele).text().substr(1));});$(".price-sum em").text("¥" + money.toFixed(2));};// 6.删除商品模块// (1)商品后面的删除按钮$(".p-action a").click(function() {// 删除的是当前的商品$(this).parents(".cart-item").remove();getSum();})// (2)删除选中的商品$(".remove-batch").click(function() {$(".j-checkbox:checked").parents(".cart-item").remove();getSum();});// (3)清空购物车删除全部商品$(".clear-all").click(function() {$(".cart-item").remove();getSum();});});

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。