Blogger Information
Blog 15
fans 0
comment 0
visits 9226
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
0114 购物车计算金额
fanyigle_php
Original
612 people have browsed it

以老师写的dom为基础,用js原生函数写的,后面在写jQuery
变化因子包括全选\反选,单品选中\反选,数量的改变(同时检查它自己的复选框是否被选中)

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>购物车自动计算</title>
  7. <link rel="stylesheet" href="style.css" />
  8. </head>
  9. <body>
  10. <table>
  11. <caption>
  12. 购物车
  13. </caption>
  14. <thead>
  15. <tr>
  16. <!-- 全选复选框 -->
  17. <th>
  18. <input
  19. type="checkbox"
  20. name="checkAll"
  21. id="check-all"
  22. checked
  23. /><label for="check-all">全选</label>
  24. </th>
  25. <th>图片</th>
  26. <th>品名</th>
  27. <th>单位</th>
  28. <th>单价/元</th>
  29. <th>数量</th>
  30. <th>金额/元</th>
  31. </tr>
  32. </thead>
  33. <tbody>
  34. <tr>
  35. <td>
  36. <input type="checkbox" name="item" value="SN-1020" checked />
  37. </td>
  38. <td>
  39. <a href=""><img src="images/p1.jpg" alt="" /></a>
  40. </td>
  41. <td>iPhone 11</td>
  42. <td></td>
  43. <td class="price">4799</td>
  44. <td><input type="number" min="1" value="1" /></td>
  45. <td class="amount">xxxx</td>
  46. </tr>
  47. <tr>
  48. <td>
  49. <input type="checkbox" name="item" value="SN-1020" checked />
  50. </td>
  51. <td>
  52. <a href=""><img src="images/p2.jpg" alt="" /></a>
  53. </td>
  54. <td>小米pro 11</td>
  55. <td></td>
  56. <td class="price">3999</td>
  57. <td><input type="number" min="1" value="2" /></td>
  58. <td class="amount">xxxx</td>
  59. </tr>
  60. <tr>
  61. <td>
  62. <input type="checkbox" name="item" value="SN-1030" checked />
  63. </td>
  64. <td>
  65. <a href=""><img src="images/p3.jpg" alt="" /></a>
  66. </td>
  67. <td>MacBook Pro</td>
  68. <td></td>
  69. <td class="price">18999</td>
  70. <td><input type="number" min="1" value="1" /></td>
  71. <td class="amount">xxxx</td>
  72. </tr>
  73. <tr>
  74. <td>
  75. <input type="checkbox" name="item" value="SN-1040" checked />
  76. </td>
  77. <td>
  78. <a href=""><img src="images/p4.jpg" alt="" /></a>
  79. </td>
  80. <td>小米75电视</td>
  81. <td></td>
  82. <td class="price">5999</td>
  83. <td><input type="number" min="1" value="2" /></td>
  84. <td class="amount">xxxx</td>
  85. </tr>
  86. <tr>
  87. <td>
  88. <input type="checkbox" name="item" value="SN-1050" checked />
  89. </td>
  90. <td>
  91. <a href=""><img src="images/p5.jpg" alt="" /></a>
  92. </td>
  93. <td>Canon 90D单反</td>
  94. <td></td>
  95. <td class="price">9699</td>
  96. <td><input type="number" min="1" value="1" /></td>
  97. <td class="amount">xxxx</td>
  98. </tr>
  99. </tbody>
  100. <tfoot>
  101. <tr style="font-weight: bolder; font-size: 1.2em">
  102. <td colspan="5">总计:</td>
  103. <td id="sum">xxxx</td>
  104. <td id="total-amount">xxxx</td>
  105. </tr>
  106. </tfoot>
  107. </table>
  108. <div style="width: 90%; margin: 10px auto">
  109. <button style="float: right; width: 100px">结算</button>
  110. </div>
  111. <script>
  112. // 1. 获取全选复选框,所有独立商品的复选框
  113. const checkAll = document.querySelector("#check-all");
  114. const checkItems = document.getElementsByName("item");
  115. // 2. 为全选复选框添加事件: change,当值改变会触发
  116. // console.log(ev.target.checked); // 看当前全选的状态
  117. checkAll.onchange = (ev) =>
  118. checkItems.forEach((item) => (item.checked = ev.target.checked));
  119. // 3. 为每个单独的商品复选框添加change
  120. checkItems.forEach(
  121. (item) =>
  122. (item.onchange = () => {
  123. checkAll.checked = [...checkItems].every((item) => item.checked);
  124. //autoCalculate();
  125. })
  126. );
  127. // =====================================================
  128. // 获取所有的数量控件
  129. const numInput = document.querySelectorAll('tbody input[type="number"');
  130. // // 用户更新数量时触发自动计算
  131. // numInput.forEach((input) => {
  132. // //单独onchange?
  133. // input.onchange = autoCalculate;
  134. // });
  135. // 购物车刚加载完成时也应该触发自动计算
  136. window.onload = autoCalculate;
  137. //表格change触发
  138. const myTable = document.querySelector("table");
  139. myTable.onchange = autoCalculate;
  140. // 封装一个自动计算的函数
  141. function autoCalculate() {
  142. // 获取单价组成的数组
  143. const prices = document.querySelectorAll("tbody .price");
  144. const priceArr = [...prices].map((item) => item.textContent * 1);
  145. console.log(priceArr);
  146. // 获取数量组成的数组
  147. const numbers = document.querySelectorAll("tbody input[type=number]");
  148. let numArr = [...numbers].map((item) => item.value * 1);
  149. console.log(numArr);
  150. // 计算每件商品金额, 单价 * 数量
  151. let amountArr = [priceArr, numArr].reduce((total, curr) =>
  152. total.map((item, index) => item * curr[index])
  153. );
  154. console.log(amountArr);
  155. // 每个商品的金额写到页面上
  156. document
  157. .querySelectorAll(".amount")
  158. .forEach((item, index) => (item.textContent = amountArr[index]));
  159. // ---------------------------------
  160. // 商品总数
  161. // 用filter过滤出选中的商品
  162. numArr = numArr.filter((item, index) => {
  163. // console.log(checkItems[index].checked);
  164. return checkItems[index].checked;
  165. });
  166. let sum = numArr.length ? numArr.reduce((pre, cur) => pre + cur) : 0;
  167. // 计算总金额
  168. // 用filter过滤出选中的商品
  169. amountArr = amountArr.filter((item, index) => {
  170. // console.log(checkItems[index].checked);
  171. return checkItems[index].checked;
  172. });
  173. let totalAmount = amountArr.length
  174. ? amountArr.reduce((pre, cur) => pre + cur)
  175. : 0;
  176. console.log(totalAmount);
  177. // 将计算结果渲染到购物车中
  178. // 总数量
  179. document.querySelector("#sum").textContent = sum;
  180. // 总金额
  181. document.querySelector("#total-amount").textContent = totalAmount;
  182. }
  183. </script>
  184. </body>
  185. </html>
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