Blogger Information
Blog 63
fans 8
comment 8
visits 50407
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP大牛成长之路:JavaScript实现购物车自动计算
周Sir-BLOG
Original
937 people have browsed it

JavaScript实现购物车自动计算

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>购物车全选</title>
  7. <style>
  8. table {
  9. border-collapse: collapse;
  10. width: 90%;
  11. text-align: center;
  12. margin: auto;
  13. }
  14. table caption {
  15. margin-bottom: 15px;
  16. font-size: 1.5rem;
  17. }
  18. table th,
  19. table td {
  20. /* border: 1px solid #888; */
  21. border-bottom: 1px solid #888;
  22. padding: 5px;
  23. }
  24. table thead tr:first-of-type {
  25. background-color: rgb(222, 222, 222);
  26. }
  27. /* 隔行变色: 偶数行 */
  28. /* table tbody tr:nth-of-type(even) {
  29. background-color: rgb(240, 240, 240);
  30. } */
  31. table input[type="checkbox"] {
  32. width: 20px;
  33. height: 20px;
  34. }
  35. button {
  36. width: 150px;
  37. height: 30px;
  38. outline: none;
  39. border: none;
  40. background-color: #f40;
  41. color: white;
  42. letter-spacing: 5px;
  43. }
  44. button:hover {
  45. background: #999;
  46. background-color: coral;
  47. cursor: pointer;
  48. }
  49. .my {
  50. color: #f40;
  51. font-weight: 700;
  52. }
  53. </style>
  54. </head>
  55. <body>
  56. <table>
  57. <caption class="my">
  58. 购物车
  59. </caption>
  60. <thead>
  61. <tr>
  62. <th>
  63. <input
  64. type="checkbox"
  65. name="checkAll"
  66. id="check-all"
  67. checked
  68. /><label for="check-all"></label>
  69. </th>
  70. <th>ID</th>
  71. <th>品名</th>
  72. <th>单位</th>
  73. <th>单价/元</th>
  74. <th>数量</th>
  75. <th>金额/元</th>
  76. </tr>
  77. </thead>
  78. <tbody id="aaa">
  79. <tr>
  80. <td>
  81. <input type="checkbox" name="itemId" value="SN-1010" checked />
  82. </td>
  83. <td>SN-1010</td>
  84. <td>MacBook Pro电脑</td>
  85. <td></td>
  86. <td>18999</td>
  87. <td>
  88. <input type="number" name="counter" value="1" min="1" step="1" />
  89. </td>
  90. <td></td>
  91. </tr>
  92. <tr>
  93. <td>
  94. <input type="checkbox" name="itemId" value="SN-1020" checked />
  95. </td>
  96. <td>SN-1020</td>
  97. <td>iPhone手机</td>
  98. <td></td>
  99. <td>4999</td>
  100. <td>
  101. <input type="number" name="counter" value="1" min="1" step="1" />
  102. </td>
  103. <td></td>
  104. </tr>
  105. <tr>
  106. <td>
  107. <input type="checkbox" name="itemId" value="SN-1030" checked />
  108. </td>
  109. <td>SN-1030</td>
  110. <td>智能AI音箱</td>
  111. <td></td>
  112. <td>399</td>
  113. <td>
  114. <input type="number" name="counter" value="1" min="1" step="1" />
  115. </td>
  116. <td></td>
  117. </tr>
  118. <tr>
  119. <td>
  120. <input type="checkbox" name="itemId" value="SN-1040" checked />
  121. </td>
  122. <td>SN-1040</td>
  123. <td>SSD移动硬盘</td>
  124. <td></td>
  125. <td>888</td>
  126. <td>
  127. <input type="number" name="counter" value="1" min="1" step="1" />
  128. </td>
  129. <td></td>
  130. </tr>
  131. <tr>
  132. <td>
  133. <input type="checkbox" name="itemId" value="SN-1050" checked />
  134. </td>
  135. <td>SN-1050</td>
  136. <td>黄山毛峰</td>
  137. <td></td>
  138. <td>999</td>
  139. <td>
  140. <input type="number" name="counter" value="1" min="1" step="1" />
  141. </td>
  142. <td></td>
  143. </tr>
  144. </tbody>
  145. <tfoot>
  146. <tr>
  147. <td colspan="5" style="font-weight: 700;">总计:</td>
  148. <td id="total-num" class="my">0</td>
  149. <td id="total-price" class="my">0</td>
  150. </tr>
  151. </tfoot>
  152. </table>
  153. <div style="width: 90%; margin: 10px auto;">
  154. <button style="float: right; width: 100px;">结算</button>
  155. </div>
  156. <script>
  157. // 全选复选框
  158. var checkAllBtn = document.querySelector("#check-all");
  159. // 单个复选框
  160. var checkItemBtns = document.getElementsByName("itemId");
  161. // 获取所有单价(单价列)
  162. var prices = document.querySelectorAll("tbody > tr > td:nth-of-type(5)");
  163. // 获取所有数量INPUT框
  164. var numbers = document.querySelectorAll("input[type=number]");
  165. // 获取所有金额(最后一列)
  166. var sumPrices = document.querySelectorAll("tbody > tr > td:nth-of-type(7)");
  167. // 商品数量(单条): 数组
  168. var number = [];
  169. // 商品金额: 数组
  170. var Price = [];
  171. // 商品总数量var
  172. var totalNum = 0;
  173. // 商品总金额
  174. var totalPrices = 0;
  175. // 载入时计算商品数量/单价/总价-----------------------------------
  176. getPrices(); //获取每行单价
  177. getTotalNum(numbers); //获取第5列商品数量
  178. getTotalPrices(sumPrices); //获取第7列商品总价
  179. //获取商品总数量(数量列相加)-------------------------------------
  180. function getTotalNum(numbers) {
  181. totalNum = 0;
  182. numbers.forEach(function (num) {
  183. var chk = num.parentNode.parentNode.children[0].childNodes[1];
  184. if (chk.checked) totalNum += parseInt(num.value);
  185. });
  186. document.querySelector("#total-num").innerText = totalNum;
  187. }
  188. //每行单价------------------------------------------------------
  189. function getPrices() {
  190. var index = 0;
  191. checkItemBtns.forEach(function (ev) {
  192. sumPrices[index].innerText =
  193. parseInt(numbers[index].value) * parseInt(prices[index].innerText);
  194. index++;
  195. });
  196. }
  197. //获取总价格(最后一列相加)--------------------------------------
  198. function getTotalPrices(sumPrices) {
  199. //总价先清零,避免累加
  200. totalPrices = 0;
  201. // 遍历最后一列
  202. sumPrices.forEach(function (price) {
  203. // 获取选中状态
  204. var chk = price.parentNode.children[0].childNodes[1];
  205. if (chk.checked) {
  206. // 商品选中总价格才相加(并设置背景高亮)
  207. totalPrices += parseInt(price.innerText);
  208. chk.parentNode.parentNode.style.cssText = "background:#fff8e1";
  209. } else {
  210. // 商品未选中取消背景高亮
  211. chk.parentNode.parentNode.style.cssText = "background:#fff";
  212. }
  213. });
  214. // 更新总价格
  215. document.querySelector("#total-price").innerText = totalPrices;
  216. }
  217. // 给表头的全选按钮添加事件--------------------------------------
  218. checkAllBtn.addEventListener("change", allChange, false);
  219. function allChange(ev) {
  220. // 遍历每个商品复选框,根据全选状态控制每个商品复选框状态
  221. checkItemBtns.forEach(function (btn) {
  222. btn.checked = ev.target.checked;
  223. });
  224. getTotalNum(numbers); //更新商品总数量
  225. getTotalPrices(sumPrices); //更新商品总金额
  226. }
  227. //每行的复选框添加监听事件---------------------------------------
  228. checkItemBtns.forEach(function (btn) {
  229. // 根据每个复选框的状态,控制全选的状态
  230. btn.addEventListener("change", itemChange, false);
  231. });
  232. function itemChange(btn) {
  233. var i = 0;
  234. //循环每个复选框,得到选中状态复选框数量
  235. checkItemBtns.forEach(function (btn) {
  236. if (btn.checked) i++;
  237. });
  238. // 设置全选按钮的状态(如果商品的复选框数量等于总的商品条目数,就将全选选中/取消)
  239. checkAllBtn.checked = i === checkItemBtns.length;
  240. getTotalNum(numbers); //更新商品总数量
  241. getTotalPrices(sumPrices); //更新商品总金额
  242. }
  243. //循环每个INPUT并添加监听事件,当商品数量发生变化,重新计算价格------
  244. numbers.forEach(function (btn) {
  245. btn.addEventListener(
  246. "input",
  247. function () {
  248. getTotalNum(numbers); //更新商品总数量
  249. getPrices(); //获取每行单价
  250. getTotalPrices(sumPrices); //更新商品总金额
  251. },
  252. false
  253. );
  254. });
  255. </script>
  256. </body>
  257. </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