博客列表 >jQuery完成购物车全选/不全选、实现勾选/不勾选商品时购物车自动完成计算

jQuery完成购物车全选/不全选、实现勾选/不勾选商品时购物车自动完成计算

吳
原创
2021年01月19日 19:28:181394浏览

1.jQuery完成购物车全选/不全选

  1. <html lang="en">
  2. <head>
  3. <meta charset="UTF-8">
  4. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  5. <title>jquery购物车全选与不全选</title>
  6. </title>
  7. <link rel="stylesheet" href="style.css" />
  8. <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
  9. </head>
  10. <body>
  11. <table>
  12. <caption>
  13. 购物车
  14. </caption>
  15. <thead>
  16. <tr>
  17. <!-- 全选复选框 -->
  18. <th><input type="checkbox" name="checkAll" id="check-all" checked /><label for="check-all">全选</label>
  19. </th>
  20. <th>图片</th>
  21. <th>品名</th>
  22. <th>单位</th>
  23. <th>单价/元</th>
  24. <th>数量</th>
  25. <th>金额/元</th>
  26. </tr>
  27. </thead>
  28. <tbody>
  29. <tr>
  30. <td>
  31. <input type="checkbox" name="item" value="SN-1020" checked />
  32. </td>
  33. <td>
  34. <a href=""><img src="images/p1.jpg" alt="" /></a>
  35. </td>
  36. <td>iPhone 11</td>
  37. <td></td>
  38. <td class="price">4799</td>
  39. <td><input type="number" min="1" value="1" /></td>
  40. <td class="amount">xxxx</td>
  41. </tr>
  42. <tr>
  43. <td>
  44. <input type="checkbox" name="item" value="SN-1020" checked />
  45. </td>
  46. <td>
  47. <a href=""><img src="images/p2.jpg" alt="" /></a>
  48. </td>
  49. <td>小米pro 11</td>
  50. <td></td>
  51. <td class="price">3999</td>
  52. <td><input type="number" min="1" value="2" /></td>
  53. <td class="amount">xxxx</td>
  54. </tr>
  55. <tr>
  56. <td>
  57. <input type="checkbox" name="item" value="SN-1030" checked />
  58. </td>
  59. <td>
  60. <a href=""><img src="images/p3.jpg" alt="" /></a>
  61. </td>
  62. <td>MacBook Pro</td>
  63. <td></td>
  64. <td class="price">18999</td>
  65. <td><input type="number" min="1" value="1" /></td>
  66. <td class="amount">xxxx</td>
  67. </tr>
  68. <tr>
  69. <td>
  70. <input type="checkbox" name="item" value="SN-1040" checked />
  71. </td>
  72. <td>
  73. <a href=""><img src="images/p4.jpg" alt="" /></a>
  74. </td>
  75. <td>小米75电视</td>
  76. <td></td>
  77. <td class="price">5999</td>
  78. <td><input type="number" min="1" value="2" /></td>
  79. <td class="amount">xxxx</td>
  80. </tr>
  81. <tr>
  82. <td>
  83. <input type="checkbox" name="item" value="SN-1050" checked />
  84. </td>
  85. <td>
  86. <a href=""><img src="images/p5.jpg" alt="" /></a>
  87. </td>
  88. <td>Canon 90D单反</td>
  89. <td></td>
  90. <td class="price">9699</td>
  91. <td><input type="number" min="1" value="1" /></td>
  92. <td class="amount">xxxx</td>
  93. </tr>
  94. </tbody>
  95. <tfoot>
  96. <tr style="font-weight: bolder; font-size: 1.2em">
  97. <td colspan="5">总计:</td>
  98. <td id="sum">xxxx</td>
  99. <td id="total-amount">xxxx</td>
  100. </tr>
  101. </tfoot>
  102. </table>
  103. <div style="width: 90%; margin: 10px auto">
  104. <button style="float: right; width: 100px">结算</button>
  105. </div>
  106. <script>
  107. // 1.全选复选框
  108. const checkAll = $("#check-all");
  109. // 2.独立复选框
  110. const checkItems = $("input[name='item']");
  111. // 改变全选,独立复选框也改变
  112. checkAll.change(ev => checkItems.each(function () {
  113. $(this).prop("checked", $(ev.target).is(":checked"));
  114. }))
  115. // 改变独立复选框,改变全选
  116. checkItems.change(() => checkAll.prop("checked", [...checkItems].every(item => item.checked)));
  117. </script>
  118. </body>
  119. </html>

2.勾选/不勾选商品时购物车自动完成计算

  • 勾选

  • 不勾选

  1. <html lang="en">
  2. <head>
  3. <meta charset="UTF-8">
  4. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  5. <title>购物车勾选不勾选时自动完成计算</title>
  6. <link rel="stylesheet" href="style.css" />
  7. </head>
  8. <body>
  9. <table>
  10. <caption>
  11. 购物车
  12. </caption>
  13. <thead>
  14. <tr>
  15. <!-- 全选复选框 -->
  16. <th><input type="checkbox" name="checkAll" id="check-all" checked /><label for="check-all">全选</label>
  17. </th>
  18. <th>图片</th>
  19. <th>品名</th>
  20. <th>单位</th>
  21. <th>单价/元</th>
  22. <th>数量</th>
  23. <th>金额/元</th>
  24. </tr>
  25. </thead>
  26. <tbody>
  27. <tr>
  28. <td>
  29. <input type="checkbox" name="item" value="SN-1020" checked />
  30. </td>
  31. <td>
  32. <a href=""><img src="images/p1.jpg" alt="" /></a>
  33. </td>
  34. <td>iPhone 11</td>
  35. <td></td>
  36. <td class="price">4799</td>
  37. <td><input type="number" min="1" value="1" /></td>
  38. <td class="amount">xxxx</td>
  39. </tr>
  40. <tr>
  41. <td>
  42. <input type="checkbox" name="item" value="SN-1020" checked />
  43. </td>
  44. <td>
  45. <a href=""><img src="images/p2.jpg" alt="" /></a>
  46. </td>
  47. <td>小米pro 11</td>
  48. <td></td>
  49. <td class="price">3999</td>
  50. <td><input type="number" min="1" value="2" /></td>
  51. <td class="amount">xxxx</td>
  52. </tr>
  53. <tr>
  54. <td>
  55. <input type="checkbox" name="item" value="SN-1030" checked />
  56. </td>
  57. <td>
  58. <a href=""><img src="images/p3.jpg" alt="" /></a>
  59. </td>
  60. <td>MacBook Pro</td>
  61. <td></td>
  62. <td class="price">18999</td>
  63. <td><input type="number" min="1" value="1" /></td>
  64. <td class="amount">xxxx</td>
  65. </tr>
  66. <tr>
  67. <td>
  68. <input type="checkbox" name="item" value="SN-1040" checked />
  69. </td>
  70. <td>
  71. <a href=""><img src="images/p4.jpg" alt="" /></a>
  72. </td>
  73. <td>小米75电视</td>
  74. <td></td>
  75. <td class="price">5999</td>
  76. <td><input type="number" min="1" value="2" /></td>
  77. <td class="amount">xxxx</td>
  78. </tr>
  79. <tr>
  80. <td>
  81. <input type="checkbox" name="item" value="SN-1050" checked />
  82. </td>
  83. <td>
  84. <a href=""><img src="images/p5.jpg" alt="" /></a>
  85. </td>
  86. <td>Canon 90D单反</td>
  87. <td></td>
  88. <td class="price">9699</td>
  89. <td><input type="number" min="1" value="1" /></td>
  90. <td class="amount">xxxx</td>
  91. </tr>
  92. </tbody>
  93. <tfoot>
  94. <tr style="font-weight: bolder; font-size: 1.2em">
  95. <td colspan="5">总计:</td>
  96. <td id="sum">xxxx</td>
  97. <td id="total-amount">xxxx</td>
  98. </tr>
  99. </tfoot>
  100. </table>
  101. <div style="width: 90%; margin: 10px auto">
  102. <button style="float: right; width: 100px">结算</button>
  103. </div>
  104. <script>
  105. // 全选复选框
  106. const checkAll = document.getElementById("check-all");
  107. const checkItems = document.getElementsByName("item");
  108. // 改变全选,独立复选框也改变
  109. checkAll.onchange = ev => checkItems.forEach(item => item.checked = ev.target.checked);
  110. // 改变独立复选框,改变全选
  111. checkItems.forEach(item => item.onchange = () => checkAll.checked = [...checkItems].every(ele => ele.checked));
  112. // 购物车,自动计算
  113. function cart() {
  114. // 独立单价组
  115. let priceArr = [...document.getElementsByClassName("price")].map(p => p.textContent * 1);
  116. // 独立数量数组
  117. let numbersArr = [...document.querySelectorAll("input[type='number']")].map(n => n.value * 1);
  118. // 独立金额数组
  119. let aumoutsArr = [priceArr, numbersArr].reduce((total, curr) => total.map((item, i) => item * curr[i]));
  120. // 独立金额数据填充,由单品数量改变时触发,与独立复选框状态无关
  121. document.querySelectorAll(".amount").forEach((ele, i) => (ele.textContent = aumoutsArr[i]));
  122. // 获取所有独立复选框元素
  123. let items = document.getElementsByName("item");
  124. // 过滤出所有独立复选框选中状态的数量数组
  125. numbersArr = numbersArr.filter((ele, i) => [...items][i].checked);
  126. // 过滤出所有独立复选框选中状态的金额数组
  127. aumoutsArr = aumoutsArr.filter((ele, i) => [...items][i].checked);
  128. // 计算总数量和总金额
  129. let sum = numbersArr.reduce((pre, cur) => pre + cur, 0);
  130. let total = aumoutsArr.reduce((pre, cur) => pre + cur, 0);
  131. // 填充总数量和总金额到页面中
  132. document.getElementById("sum").textContent = sum;
  133. document.getElementById("total-amount").textContent = total;
  134. }
  135. // 改变单品数量时触发
  136. document.querySelectorAll("input[type='number']").forEach(ele => ele.onchange = cart);
  137. // 改变独立复选框状态时触发
  138. document.getElementsByName("item").forEach(item => item.onchange = cart);
  139. // 全选状态改变时触发
  140. document.getElementById("check-all").addEventListener("change", cart);
  141. // 页面载入完成时加载
  142. window.onload = cart;
  143. </script>
  144. </body>
  145. </html>
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议