经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » JS/JS库/框架 » jQuery » 查看文章
jQuery实现购物车全功能
来源:jb51  时间:2021/1/11 10:47:54  对本文有异议

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

效果图:

HTML&&CSS:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3.  
  4. <head>
  5. <meta charset="UTF-8">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. <script src="../jquery-3.4.1.min.js"></script>
  9. <style>
  10. * {
  11. margin: 0;
  12. padding: 0;
  13. }
  14. .tab {
  15. width: 500px;
  16. border-collapse: collapse;
  17. margin: 0 auto;
  18. }
  19. .tab td,
  20. th {
  21. border: 1px solid #000;
  22. }
  23. .tab .num {
  24. width: 20px;
  25. }
  26. .tab .add,
  27. .sub {
  28. width: 20px;
  29. }
  30. .current {
  31. background-color: pink;
  32. }
  33. </style>
  34. </head>
  35.  
  36. <body>
  37. <table class="tab">
  38. <thead>
  39. <th>全选 <input type="checkbox" name="" value="" class="checkAll">
  40. <input type="checkbox" name="" value="" class="checkAll">
  41. </th>
  42. <th>商品名称</th>
  43. <th>单价</th>
  44. <th>数量</th>
  45. <th>小计</th>
  46. <th>操作</th>
  47. </thead>
  48. <tbody>
  49. <tr>
  50. <td><input type="checkbox" class="ed" /></td>
  51. <td>电脑</td>
  52. <td class="price">¥200.20</td>
  53. <td>
  54. <button type="button" class="sub">-</button>
  55. <input type="text" name="" value="1" class="num">
  56. <button type="button" class="add">+</button>
  57. </td>
  58. <td class="small_total">¥200.20</td>
  59. <td class="delete">删除</td>
  60. </tr>
  61. <tr>
  62. <td><input type="checkbox" class="ed" /></td>
  63. <td>手机</td>
  64. <td class="price">¥100.30</td>
  65. <td>
  66. <button type="button" class="sub">-</button>
  67. <input type="text" name="" value="1" class="num">
  68. <button type="button" class="add">+</button>
  69. </td>
  70. <td class="small_total">¥100.30</td>
  71. <td class="delete">删除</td>
  72. </tr>
  73. <tr>
  74. <td><input type="checkbox" class="ed" /></td>
  75. <td>空调</td>
  76. <td class="price">¥1000.99</td>
  77. <td>
  78. <button type="button" class="sub">-</button>
  79. <input type="text" name="" value="1" class="num">
  80. <button type="button" class="add">+</button>
  81. </td>
  82. <td class="small_total">¥1000.99</td>
  83. <td class="delete">删除</td>
  84. </tr>
  85. </tbody>
  86. </table>
  87. <div>
  88. <span>已选<span style="color: red;" class="num_sum">1</span>件商品</span>
  89. <span>总计:</span>
  90. <span class="sum" style="color: red;">0</span>
  91. <div><span style="color: red;" class="delSome">删除选中商品</span>
  92. <span style="color: red;" class="delAll">清空购物车</span>
  93. </div>
  94. </div>
  95. </body>
  96.  
  97. </html>

JS:

  1. //里面三个小的复选按钮选中状态跟着 全选按钮走
  2. //因为checked是复选框的固有属性,此时利用prop()获取和设置该属性
  3. $(function() {
  4. getSum();
  5. $(".checkAll").change(function() {
  6. // console.log($(this).prop("checked"));//全选按钮的状态
  7. $(".ed,.checkAll").prop("checked", $(this).prop("checked"));
  8. getSum();
  9. if ($(".ed,.checkAll").prop("checked")) {
  10. //如果全选,让所有商品添加类名(背景颜色)
  11. $(".tab tbody").children().addClass("current");
  12. } else {
  13. $(".tab tbody").children().removeClass("current");
  14. }
  15. })
  16. //如果所有小按钮的个数都被选了,全选按钮就选上,如果小按钮没有被选上,则全选按钮就不选上
  17. //:checked选择器,查找本选中的表单元素
  18. $(".ed").change(function() {
  19. // console.log($(".ed:checked").length);//小复选框选中的个数
  20. // console.log($(".ed").length);
  21. //console.log($(this).prop("checked"));
  22. if ($(".ed:checked").length === $(".ed").length) {
  23. $(".checkAll").prop("checked", true);
  24. } else {
  25. $(".checkAll").prop("checked", false);
  26. }
  27. getSum();
  28. if ($(this).prop("checked")) {
  29. $(this).parents("tr").addClass("current");
  30. } else {
  31. $(this).parents("tr").removeClass("current");
  32. }
  33. })
  34.  
  35. $(".add").click(function() {
  36. let n = parseInt($(this).siblings(".num").val());
  37. //console.log(n);
  38. n++;
  39. $(this).siblings(".num").val(n);
  40. let price = $(this).parent().siblings(".price").html();
  41. price = price.substr(1);
  42. //console.log(price);
  43. $(this).parent().siblings(".small_total").text("¥" + (n * price).toFixed(2));
  44. getSum();
  45. })
  46. $(".sub").click(function() {
  47. let n = parseInt($(this).siblings(".num").val());
  48. //console.log(n);
  49. if (n === 1) {
  50. return false;
  51. }
  52. n--;
  53. $(this).siblings(".num").val(n);
  54. let price = $(this).parent().siblings(".price").html();
  55. price = price.substr(1);
  56. //console.log(price);
  57. $(this).parent().siblings(".small_total").text("¥" + (n * price).toFixed(2));
  58. getSum();
  59. })
  60. //用户也可以直接修改表单num里面的值(小bug),同样计算小计
  61. $(".num").change(function() {
  62. let n = $(this).val();
  63. let price = $(this).parent().siblings(".price").html();
  64. price = price.substr(1);
  65. $(this).parent().siblings(".small_total").text("¥" + (n * price).toFixed(2));
  66. getSum();
  67. })
  68.  
  69. function getSum() {
  70. let count = 0; //计算总件数
  71. let money = 0; //计算总价钱
  72. $(".num").each(function(index) {
  73. if ($(".ed").eq(index).prop("checked") == true) {
  74. count += parseInt($(".num").eq(index).val());
  75. money += parseFloat($(".small_total").eq(index).text().substr(1));
  76. }
  77. })
  78. $(".num_sum").html(count);
  79. $(".sum").html(money.toFixed(2));
  80. }
  81.  
  82. //删除商品模块
  83. //点击删除之后一定是删除当前的商品,所以从$(this)出发
  84. $(".delete").click(function() {
  85. //删除的是当前的商品
  86. $(this).parent().remove();
  87. $(".ed").change();
  88. getSum();
  89. clearCheckAll();
  90. })
  91. //删除选定的商品:小的复选框如果选中就删除对应的商品
  92. $(".delSome").click(function() {
  93. //删除的是选中的商品
  94. $(".ed:checked").parent().parent().remove();
  95. getSum();
  96. clearCheckAll();
  97. })
  98. //清空购物车
  99. $(".delAll").click(function() {
  100. $(".tab tbody").empty();
  101. getSum();
  102. clearCheckAll();
  103. })
  104.  
  105. function clearCheckAll() {
  106. if ($(".tab tbody")[0].innerText == '') {
  107. $(".checkAll").prop("checked", false);
  108. }
  109. }
  110. })

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

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号