购物车功能

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  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. </head>
  10. <body>
  11. <table>
  12. <caption>购物车</caption>
  13. <thead>
  14. <tr>
  15. <!-- 全选复选框 -->
  16. <th>
  17. <input type="checkbox" name="checkAll" id="check-all" />
  18. <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" data-index="0" />
  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">0</td>
  41. </tr>
  42. <tr>
  43. <td>
  44. <input type="checkbox" name="item" value="SN-1020" data-index="1" />
  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">0</td>
  54. </tr>
  55. <tr>
  56. <td>
  57. <input type="checkbox" name="item" value="SN-1030" data-index="2" />
  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">0</td>
  67. </tr>
  68. <tr>
  69. <td>
  70. <input type="checkbox" name="item" value="SN-1040" data-index="3" />
  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">0</td>
  80. </tr>
  81. <tr>
  82. <td>
  83. <input type="checkbox" name="item" value="SN-1050" data-index="4" />
  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">0</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">0</td>
  99. <td id="total-amount">0</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. // 获取全选按钮,每个独立的商品复选框
  108. const checkAll = document.querySelector("#check-all");
  109. const checkItems = document.getElementsByName("item");
  110. // 将当前全选的状态变化赋值给每个商品
  111. checkAll.onchange = (ev) => {
  112. checkItems.forEach((item) => (item.checked = ev.target.checked));
  113. if (ev.target.checked) {
  114. totallPrices(checkedArr());
  115. } else {
  116. clearCalculate();
  117. }
  118. };
  119. // 为每个单独的商品复选框添加 change
  120. checkItems.forEach(
  121. (item) =>
  122. (item.onchange = () => {
  123. checkAll.checked = [...checkItems].every((item, index) => item.checked);
  124. totallPrices(checkedArr());
  125. })
  126. );
  127. // 获取数量 input 元素
  128. const numInput = document.querySelectorAll('tbody input[type="number"]');
  129. // 用户更新数量时自动计算
  130. numInput.forEach((input) => (input.onchange = goodPrice));
  131. // 页面加载完成后自动计算单个商品的总价
  132. window.onload = goodPrice();
  133. // 获取被选中的复选框数组
  134. function checkedArr() {
  135. var id = document.getElementsByName("item");
  136. var value = [];
  137. for (var i = 0; i < id.length; i++) {
  138. if (id[i].checked) {
  139. value.push(id[i].dataset.index);
  140. }
  141. }
  142. return { goodIndex: value, totallType: id.length };
  143. }
  144. // 计算单个商品的总价
  145. function goodPrice() {
  146. // 价格数组
  147. const prices = document.querySelectorAll("tbody .price");
  148. const priceArr = [...prices].map((price) => price.textContent * 1);
  149. // 数量数组
  150. const numbers = document.querySelectorAll("tbody input[type='number']");
  151. const numArr = [...numbers].map((num) => num.value * 1);
  152. // 计算金额:数量 * 单价 = 金额
  153. const amountArr = [priceArr, numArr].reduce((total, curr) => total.map((item, index) => item * curr[index]));
  154. // 将每个商品的金额渲染到页面
  155. document.querySelectorAll(".amount").forEach((item, index) => (item.textContent = amountArr[index]));
  156. }
  157. // 计算选中总价和总数
  158. function totallPrices(checkItemsArr) {
  159. console.log(checkItemsArr);
  160. // 如果为空数组时,数量和总价改为 0
  161. if (checkItemsArr["goodIndex"].length <= 0) {
  162. clearCalculate();
  163. return;
  164. }
  165. const prices = document.querySelectorAll("tbody .price");
  166. const numbers = document.querySelectorAll("tbody input[type='number']");
  167. // 价格数组
  168. let priceArr = [...prices].map((price) => price.textContent * 1);
  169. // 数量数组
  170. let numArr = [...numbers].map((num) => num.value * 1);
  171. // 获取选中的商品数量和数组
  172. // 程序目前报错。。。。。。。。。
  173. // if (checkItemsArr["goodIndex"].length <= checkItemsArr["totallType"]) {
  174. // let nowPriceArr, nowNumArr;
  175. // for (i = 0; i < checkItemsArr["goodIndex"].length; i++) {
  176. // index = Number(checkItemsArr["goodIndex"][i]);
  177. // nowPriceArr[i] = priceArr[i];
  178. // nowNumArr[i] = numArr[index];
  179. // }
  180. // priceArr = nowPriceArr;
  181. // numArr = nowNumArr;
  182. // }
  183. // 商品总数
  184. let sum = numArr.reduce((pre, cur) => pre + cur);
  185. // 计算金额:数量 * 单价 = 金额
  186. const amountArr = [priceArr, numArr].reduce((total, curr) => total.map((item, index) => item * curr[index]));
  187. // 将总数量渲染到页面
  188. document.querySelector("#sum").textContent = String(sum);
  189. // 将总金额渲染到页面
  190. document.querySelector("#total-amount").textContent = String(amountArr.reduce((pre, curr) => pre + curr));
  191. }
  192. // 如果没有选择商品,商品数量和单价为零
  193. function clearCalculate() {
  194. // 将总数量渲染到页面
  195. document.querySelector("#sum").textContent = "0";
  196. // 将总金额渲染到页面
  197. document.querySelector("#total-amount").textContent = "0";
  198. }
  199. </script>
  200. </body>
  201. </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. }

更多相关文章

  1. 0415作业-Vue常用指令及方法
  2. 为留言板添加字数实时统计与禁止超出功能; 2. 自选一些字符串和
  3. 留言板,字符串和数组方法 ----0407
  4. js之购物车自动计算
  5. 当面试官问我ArrayList和LinkedList哪个更占空间时,我这么答让他
  6. 悟透前端:javascript数组之includes、reduce
  7. JavaScript数组扁平化的黑科技
  8. 第14部分- Linux ARM汇编数组/结构体/索引
  9. C语言指针全归纳-初级版

随机推荐

  1. 开开心心爬APP,结果一坑连一坑
  2. nginx跳转 去掉工程名
  3. #6.1# 用python画出你的童年回忆
  4. centos7 中yum不能用了
  5. 看了我做的年度报表,老板大呼NB!
  6. 摆摊吧,后浪
  7. 用python爬取4332条粽子数据进行分析
  8. 一个公式三指标,电商分析的破局之道
  9. Python爬取 201865 条《隐秘的角落》弹幕
  10. LeetCode第 146 号问题: LRU 缓存机制