Blogger Information
Blog 40
fans 0
comment 1
visits 34274
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
JavaScript学习之购物车全选、自动计算
景云
Original
807 people have browsed it

功能示例图

1.商品的全选与取消全选

1.1 点击全选时,每个商品选择框全部激活;反之全部取消。
1.2 点击单个商品选择框时,会判断所所有商品选择框是否全部激活,全部激活则全选框激活,只要有一个选择框未激活,则全选框不激活。

2. 商品金额的计算

2.1 每个商品根据数量和单价计算单个商品总额,渲染到页面中

3.根据商品选择框是否激活,计算已激活的所以商品的数量和金额,渲染到页面中

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>购物车全选与自动计算</title>
  8. <link rel="stylesheet" href="./style.css">
  9. <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.js"></script>
  10. </head>
  11. <body>
  12. <table>
  13. <caption>
  14. 购物车
  15. </caption>
  16. <thead>
  17. <tr>
  18. <!-- 全选复选框 -->
  19. <th><input type="checkbox" name="checkAll" id="check-all" /><label for="check-all">全选</label></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/1.gif" alt="" /></a>
  35. </td>
  36. <td>iPhone 11</td>
  37. <td></td>
  38. <td class="price">1</td>
  39. <td><input type="number" min="1" value="1" /></td>
  40. <td class="amount">xxx</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/1.gif" alt="" /></a>
  48. </td>
  49. <td>小米pro 11</td>
  50. <td></td>
  51. <td class="price">2</td>
  52. <td><input type="number" min="1" value="2" /></td>
  53. <td class="amount">xxx</td>
  54. </tr>
  55. <tr>
  56. <td>
  57. <input type="checkbox" name="item" value="SN-1030" />
  58. </td>
  59. <td>
  60. <a href=""><img src="images/1.gif" alt="" /></a>
  61. </td>
  62. <td>MacBook Pro</td>
  63. <td></td>
  64. <td class="price">3</td>
  65. <td><input type="number" min="1" value="1" /></td>
  66. <td class="amount">xxx</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/1.gif" alt="" /></a>
  74. </td>
  75. <td>小米75电视</td>
  76. <td></td>
  77. <td class="price">4</td>
  78. <td><input type="number" min="1" value="2" /></td>
  79. <td class="amount">xxx</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/1.gif" alt="" /></a>
  87. </td>
  88. <td>Canon 90D单反</td>
  89. <td></td>
  90. <td class="price">5</td>
  91. <td><input type="number" min="1" value="1" /></td>
  92. <td class="amount">xxx</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">xxx</td>
  99. <td id="total-amount">xxx</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 = document.querySelector("#check-all");
  109. const checkItems = document.getElementsByName("item");
  110. // 为全选框添加事件:change(当值发生变化时会触发)
  111. checkAll.onchange = function (ev) {
  112. checkItems.forEach(function (item) {
  113. item.checked = ev.target.checked;
  114. })
  115. //全选框取消或激活时需要触发自动计算
  116. autoCalculate();
  117. }
  118. // 为每个单独的商品选择框添加change事件(每个商品都选择时,全选框被激活,否则取消)
  119. checkItems.forEach(function (item) {
  120. item.onchange = function () {
  121. // Array.every(callback):对数组中每个成员进行回调处理,只有全部为true,最终才返回true,只要有一个返回false最终也会返回false,类似“逻辑与 and”;some()相反。
  122. checkAll.checked = [...checkItems].every(item => item.checked);
  123. //每次选中或取消单个商品选择框时需要触发自动计算
  124. autoCalculate();
  125. }
  126. })
  127. // 获取所以的数量控件
  128. const numInput = document.querySelectorAll("tbody input[type=number]");
  129. // 用户更新数量时自动触发自动计算
  130. numInput.forEach(function (input) {
  131. input.onchange = autoCalculate;
  132. })
  133. // 购物车刚加载完也要触发自动计算
  134. window.onload = autoCalculate;
  135. // 封装一个计算金额的函数
  136. function autoCalculate() {
  137. // 获取单价组成的数组
  138. const prices = document.querySelectorAll("tbody .price");
  139. const priceArr = [...prices].map(function (item) {
  140. return item.textContent * 1;//结果为字符串,乘以1后会执行自动转换,转换成数字
  141. })
  142. // 获取数量组成的数组
  143. const numbers = document.querySelectorAll("tbody input[type=number]");
  144. const numberArr = [...numbers].map(function (item) {
  145. return item.value * 1;//结果为字符串,乘以1后会执行自动转换,转换成数字
  146. })
  147. //计算每件商品金额,单价*数量
  148. // 将金额数组和数量数组作为一个新的数组,reduce()将会对其内的两个数组都进行处理; total为最终返回值,curr为当前被操作的数组
  149. const amountArr = [priceArr, numberArr].reduce(function (total, curr) {
  150. return total.map(function (item, index) {
  151. return item * curr[index];
  152. })
  153. })
  154. //获取选中的商品数量的数组
  155. let checkNumberArr = [];
  156. [...numbers].map(function (item, index) {
  157. // 如果当前商品选中,将商品数量取出
  158. if (checkItems[index].checked) {
  159. checkNumberArr[index] = item.value * 1;
  160. }
  161. })
  162. //获取选中的商品价格的数组
  163. let checkPriceArr = [];
  164. [...prices].map(function (item, index) {
  165. // 如果当前商品选中,将商品数量取出
  166. if (checkItems[index].checked) {
  167. checkPriceArr[index] = item.textContent * 1;
  168. }
  169. })
  170. //计算每件选中商品的金额
  171. const amountCheckedArr = [checkPriceArr, checkNumberArr].reduce(function (total, curr) {
  172. return total.map(function (item, index) {
  173. return item * curr[index];
  174. })
  175. })
  176. //选中商品总数
  177. let sum = 0;
  178. if (checkNumberArr.length > 0) {
  179. sum = checkNumberArr.reduce(function (pre, cur) {
  180. return pre + cur;
  181. })
  182. }
  183. // 计算选中商品总金额
  184. let totalAmount = 0;
  185. if (amountCheckedArr.length > 0) {
  186. totalAmount = amountCheckedArr.reduce(function (pre, cur) {
  187. return pre + cur;
  188. });
  189. }
  190. // 将计算的结果渲染到购物车中
  191. document.querySelector("#sum").textContent = sum;
  192. // 总金额也渲染
  193. if (totalAmount) {
  194. document.querySelector("#total-amount").textContent = totalAmount;
  195. } else {
  196. document.querySelector("#total-amount").textContent = '0';
  197. }
  198. // 每个商品的金额也渲染
  199. document.querySelectorAll(".amount").forEach(function (item, index) {
  200. item.textContent = amountArr[index];
  201. });
  202. }
  203. </script>
  204. </body>
  205. </html>

CSS

  1. table {
  2. border-collapse: collapse;
  3. width: 90%;
  4. text-align: center;
  5. margin: auto;
  6. }
  7. table caption {
  8. margin-bottom: 15px;
  9. font-size: 1.5rem;
  10. }
  11. table th,
  12. table td {
  13. border-bottom: 1px solid #ccc;
  14. padding: 5px;
  15. font-weight: normal;
  16. }
  17. table thead tr:first-of-type {
  18. background-color: #e6e6e6;
  19. height: 3em;
  20. }
  21. table input[type="checkbox"] {
  22. width: 1.5em;
  23. height: 1.5em;
  24. }
  25. table tbody tr {
  26. border-bottom: 1px solid #ccc;
  27. }
  28. table tbody tr:hover {
  29. background-color: #f6f6f6;
  30. cursor: pointer;
  31. }
  32. tbody img {
  33. width: 3em;
  34. }
  35. tbody input[type="number"] {
  36. width: 3em;
  37. }
  38. button {
  39. width: 150px;
  40. height: 30px;
  41. outline: none;
  42. border: none;
  43. background-color: teal;
  44. color: white;
  45. letter-spacing: 5px;
  46. }
  47. button:hover {
  48. opacity: 0.7;
  49. cursor: pointer;
  50. }
Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!