当前位置:首页 > Web开发 > 正文

false); //else全选按钮不被选中TotalPrice();}} else { //如果店铺按钮不被选中$(

2024-03-31 Web开发

购物车示例js,为了便利参考,页面写的对照简单。示例如下图所示:

技术图片

html代码如下: <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <style> .product{ width: 450px; } .product-price{ float: right; } .total{ float: right; } input[type=‘text‘]{ width: 50px; } </style> <title>购物车结算</title> </head> <body> <div> <!--第一个店铺--> <div>店铺1</div> <div> <!--第一个商品--> <div><span>商品A</span> <input type="checkbox"/> <button>-</button> <input type="text" value="1"/> <button>+</button> <div> <span>单价:¥</span><b>10</b> <span>总计:¥</span><b>0.00</b></div> </div> <!--第二个商品--> <div><span>商品B</span> <input type="checkbox"/> <button>-</button> <input type="text" value="1"/> <button>+</button> <div> <span>单价:¥</span><b>19.99</b> <span>总计:¥</span><b>0.00</b></div> </div> <!--第三个商品--> <div><span>商品C</span> <input type="checkbox"/> <button>-</button> <input type="text" value="1"/> <button>+</button> <div> <span>单价:¥</span><b>19.99</b> <span>总计:¥</span><b>0.00</b></div> </div> <div> <input type="checkbox"/> <span>店铺全选 本店合计:¥</span> <b>0.00</b> </div> </div> <div>店铺2</div> <div> <!--第一个商品--> <div><span>商品D</span> <input type="checkbox"/> <button>-</button> <input type="text" value="1"/> <button>+</button> <div> <span>单价:¥</span><b>19.99</b> <span>总计:¥</span><b>0.00</b></div> </div> <!--第二个商品--> <div><span>商品E</span> <input type="checkbox"/> <button>-</button> <input type="text" value="1"/> <button>+</button> <div> <span>单价:¥</span><b>19.99</b> <span>总计:¥</span><b>0.00</b></div> </div> <!--第三个商品--> <div><span>商品F</span> <input type="checkbox"/> <button>-</button> <input type="text" value="1"/> <button>+</button> <div> <span>单价:¥</span><b>19.99</b> <span>总计:¥</span><b>0.00</b></div> </div> <div> <input type="checkbox"/> <span>店铺全选 本店合计:¥</span> <b>0.00</b> </div> </div> <div> <input type="checkbox"/> <span>全选 合计:¥</span> <b>0.00</b> <span>共计</span> <b>0</b> <span>件商品</span> </div> </div> </div> </body> </html> jquery代码如下: <script src="http://code.jquery.com/jquery-2.2.0.min.js"></script> <script> //商品数量输入 $(".num").keyup(function(){ var tmptxt=$(this).val(); $(this).val(tmptxt.replace(/\D|^0/g,‘‘));//限制输入的内容,只能是1-9的数字 }).bind("paste",function(){ var tmptxt=$(this).val(); $(this).val(tmptxt.replace(/\D|^0/g,‘‘));//限制复制的内容,只能是1-9的数字 }).bind("input propertychange",function(){ var price= 0 ; price=parseFloat($(this).parents(".product").find(".product-one-price").text() * $(this).val()); if(isNaN(price)){ price=$(this).parents(".product").find(".product-one-price").text() * 1;//若输入的不为数字默认为1 } $(this).parents(".product").find(".product-total-price").text(price.toFixed(2)); //显示当选中商品的总价 TotalPrice(); }).blur(function(){ if($(this).val()==‘‘) { $(this).val(1);//如果输入为0,掉去焦点时默认为1 } var price= 0 ; price=parseFloat($(this).parents(".product").find(".product-one-price").text() * $(this).val()); if(isNaN(price)){ price=$(this).parents(".product").find(".product-one-price").text() * 1;//若输入的不为数字默认为1 } $(this).parents(".product").find(".product-total-price").text(price.toFixed(2)); //显示当选中商品的总价 TotalPrice(); }); // 数量减 $(".min").click(function() { var t = $(this).parent().find(‘.num‘); t.val(parseInt(t.val()) - 1); if (t.val() <= 1) { t.val(1); } var price=0; price=$(this).parent(".product").find(".product-one-price").text() * t.val(); $(this).parent(".product").find(".product-total-price").text(price.toFixed(2)); TotalPrice(); }); // 数量加 $(".max").click(function() { var t = $(this).parent().find(‘.num‘); t.val(parseInt(t.val()) + 1); if (t.val() <= 1) { t.val(1); } var price=0; price=$(this).parent(".product").find(".product-one-price").text() * t.val(); $(this).parent(".product").find(".product-total-price").text(price.toFixed(2)); TotalPrice(); }); // 点击商品按钮 $(".product-check").click(function() { //closest(最靠近的) var goods = $(this).closest(".store").find(".product-check"); //获取本店铺的所有商品 var goodsC = $(this).closest(".store").find(".product-check:checked"); //获取本店铺所有当选中的商品 var shopC = $(this).closest(".store").find(".store-check"); //获取本店铺的全选按钮 if (goods.length == goodsC.length) { //如果选中的商品即是所有商品 shopC.prop(‘checked‘, true); //店铺全选按钮当选中 if ($(".store-check").length == $(".store-check:checked").length) { //如果店铺当选中的数量即是所有店铺的数量 $(".total-check").prop(‘checked‘, true); //全选按钮当选中 TotalPrice(); } else { $(".total-check").prop(‘checked‘, false); //else全选按钮不当选中 TotalPrice(); } } else { //如果选中的商品不即是所有商品 shopC.prop(‘checked‘, false); //店铺全选按钮不当选中 $(".total-check").prop(‘checked‘, false); //全选按钮也不当选中 // 计算 TotalPrice(); // 计算 } }); // 点击店铺按钮 $(".store-check").change(function() { if ($(this).prop("checked") == true) { //如果店铺按钮当选中 $(this).parents(".store").find(".product-check").prop(‘checked‘, true); //店铺内的所有商品按钮也当选中 if ($(".store-check").length == $(".store-check:checked").length) { //如果店铺当选中的数量即是所有店铺的数量 $(".total-check").prop(‘checked‘, true); //全选按钮当选中 TotalPrice(); } else { $(".total-check").prop(‘checked‘, false); //else全选按钮不当选中 TotalPrice(); } } else { //如果店铺按钮不当选中 $(this).parents(".store").find(".product-check").prop(‘checked‘, false); //店铺内的所有商品也不被全选 $(".total-check").prop(‘checked‘, false); //全选按钮也不当选中 TotalPrice(); } }); //点击全选按钮 $(".total-check").click(function() { if ($(this).prop("checked") == true) { //如果全选按钮当选中 $(".product-check").prop(‘checked‘, true); //所有商品都当选中 $(".store-check").prop(‘checked‘, true); //所有店铺都当选中 TotalPrice(); } else { $(".product-check").prop(‘checked‘, false); //else所有商品按钮不全选 $(".store-check").prop(‘checked‘, false); //所有店铺按钮不全选 TotalPrice(); } }); function TotalPrice() { var allprice = 0; //总价 $(".store").each(function() { //循环每个店铺 var oprice = 0; //店铺总价 $(this).find(".product-check").each(function() { //循环店铺里面的商品 var total=0;//单个商品的总价 if ($(this).is(":checked")) { //如果该商品当选中 var num = parseInt($(this).parents(".product").find(".num").val()); //得到商品的数量 var price = parseFloat($(this).parents(".product").find(".product-one-price").text()); //得到商品的单价 var total = price * num; //计算单个商品的总价 if(isNaN(total)){ total = price * 1; } oprice += total; //计算该店铺的总价 } $(this).closest(".store").find(".store-total-price").text(oprice.toFixed(2)); //显示当选中商品的店铺总价 }); var oneprice = parseFloat($(this).find(".store-total-price").text()); //得到每个店铺的总价 allprice += oneprice; //计算所有店铺的总价 }); $(".total-product-num").text($(".product-check:checked").length);//共计几件商品 $(".total-price").text(allprice.toFixed(2)); //输出全部总价 } </script>

温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/web/31233.html