JS-购物车
2021/8/24 6:08:41
本文主要是介绍JS-购物车,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/css/bootstrap.min.css" integrity="sha384-B0vP5xmATw1+K9KRQjQERJvTumQW0nPEzvF6L/Z6nronJ3oUOFUFpCjEUQouq2+l" crossorigin="anonymous"> <script src="jquery-3.2.1.min.js"></script> </head> <body> <div id="main"> <table class="table table-striped"> <thead> <tr> <th scope="col">标号</th> <th scope="col">名称</th> <th scope="col">价格</th> <th scope="col">数量</th> <th scope="col">小计</th> <th scope="col">操作</th> </tr> </thead> <tbody> <tr> <th scope="row"> <input type="checkbox" name="ck" class="ck"> </th> <td>商品一号</td> <td>9.91</td> <td> <button class="btn-up">+</button> <input type="text" name="num" style="width: 30px" value="1"> <button class="btn-down">-</button> </td> <td class="subtotal">9.91</td> <td><button class="btn-danger">删除</button></td> </tr> <tr> <th scope="row"> <input type="checkbox" name="ck" class="ck"> </th> <td>商品二号</td> <td>9.9</td> <td> <button class="btn-up">+</button> <input type="text" name="num" style="width: 30px" value="1"> <button class="btn-down">-</button> </td> <td class="subtotal">9.9</td> <td><button class="btn-danger">删除</button></td> </tr> <tr> <th scope="row"> <input type="checkbox" name="ck" class="ck"> </th> <td>商品三号</td> <td>100.23</td> <td> <button class="btn-up">+</button> <input type="text" name="num" style="width: 30px" value="1"> <button class="btn-down">-</button> </td> <td class="subtotal">100.23</td> <td><button class="btn-danger">删除</button></td> </tr> </tbody> </table> <button id="qx">全选</button> <button id="qbx">全不选</button> <button id="fx">反选</button> <button id="plsc">批量删除</button> <p style="float: right"> 总共购买<span id="counter">0</span> 件 ;支付总金额:<span id="total">0.00</span>¥ </p> </div> </body> </html> <script> //加 $(".btn-up").click(function () { var num = $(this).next().val(); num++; var price = $(this).parent().prev().text(); var subTotal = price * num; $(this).next().val(num); $(this).parent().next().html(subTotal.toFixed(2)); $(this).parent().siblings(":first").find('.ck').prop("checked","checked"); computed(); }); //减 $(".btn-down").click(function () { var num = $(this).prev().val(); if(num>1){ num--; } var price = $(this).parent().prev().text(); var subTotal = price * num; $(this).prev().val(num); $(this).parent().next().text(subTotal.toFixed(2)); computed(); }); //计算总金额 function computed(){ var total = 0; var ele = $("input[name='ck']:checked"); var counter = ele.length; ele.each(function (index,v) { // total += parseFloat( $(v).parent().parent().find('.subtotal').text()); }); $("#counter").html(counter); $("#total").html(total).toFixed(2); } $(".ck").click(function () { computed(); }); //全选 $("#qx").click(function () { var ck = $("input[name='ck']"); for(var i=0;i<ck.length;i++){ ck[i].checked = true; } computed(); }); //全不选 $("#qbx").click(function () { var ck = $("input[name='ck']"); for(var i=0;i<ck.length;i++){ ck[i].checked = false; } computed(); }); //反选 $("#fx").click(function () { var ck = $("input[name='ck']"); for (var i=0;i<ck.length;i++){ if(ck[i].checked){ ck[i].checked = false; }else{ ck[i].checked = true; } } computed(); }); $(".btn-danger").click(function () { $(this).parent().parent().empty(); }); $("#plsc").click(function () { var ele = $("input[name='ck']:checked"); for(var i=0;i<ele.length;i++){ //$(ele[i]).parent().parent().remove(); $(ele[i]).parent().parent().remove(); } computed(); }); </script>
这篇关于JS-购物车的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-15useCallback教程:React Hook入门与实践
- 2024-11-15React中使用useContext开发:初学者指南
- 2024-11-15拖拽排序js案例详解:新手入门教程
- 2024-11-15React中的自定义Hooks案例详解
- 2024-11-14受控组件项目实战:从零开始打造你的第一个React项目
- 2024-11-14React中useEffect开发入门教程
- 2024-11-14React中的useMemo教程:从入门到实践
- 2024-11-14useReducer开发入门教程:轻松掌握React中的useReducer
- 2024-11-14useRef开发入门教程:轻松掌握React中的useRef用法
- 2024-11-14useState开发:React中的状态管理入门教程