博客列表 >购物车(计算选中商品)

购物车(计算选中商品)

晨
原创
2022年04月09日 20:24:56326浏览

shopping-cart.html

  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. <style>
  9. .box {
  10. width: 22em;
  11. height: 2em;
  12. }
  13. .list>li {
  14. height: 1.6em;
  15. background-color: #efefef;
  16. display: grid;
  17. grid-template-columns: repeat(5, 3em);
  18. gap: 1em;
  19. place-items: center right;
  20. border-bottom: 1px solid #ccc;
  21. }
  22. .list>li:first-of-type {
  23. background-color: lightseagreen;
  24. color: white;
  25. }
  26. .list>li:hover:not(:first-of-type) {
  27. cursor: pointer;
  28. background-color: lightcyan;
  29. }
  30. .list>li input[type='number'] {
  31. width: 3em;
  32. border: none;
  33. outline: none;
  34. text-align: center;
  35. font-size: 1em;
  36. background-color: transparent;
  37. }
  38. .list>li:last-of-type span.total-num,
  39. .list>li:last-of-type span.total-amount {
  40. grid-column: span 2;
  41. place-self: center right;
  42. color: lightseagreen;
  43. }
  44. .account {
  45. float: right;
  46. background-color: lightseagreen;
  47. color: white;
  48. border: none;
  49. outline: none;
  50. width: 4.5em;
  51. height: 1.8em;
  52. }
  53. .account:hover {
  54. background-color: coral;
  55. cursor: pointer;
  56. }
  57. </style>
  58. </head>
  59. <body>
  60. <div class="box">
  61. <div class="selectAll">
  62. <!-- checkAll(): 当全选按钮的状态发生变化化,触发该事件 change -->
  63. <input type="checkbox" class="check-all" name="check-all" onchange="checkAll()" checked />
  64. <label for="check-all">全选</label>
  65. </div>
  66. <ul class="list">
  67. <li><span>选择</span><span>品名</span><span>数量</span><span>单价</span><span>金额</span></li>
  68. <li>
  69. <input type="checkbox" onchange="checkItems()" checked />
  70. <span class="content">手机</span>
  71. <input type="number" value="1" min="1" class="num" />
  72. <span class="price">100</span>
  73. <span class="amount">0</span>
  74. </li>
  75. <li>
  76. <input type="checkbox" onchange="checkItems()" checked />
  77. <span class="content">电脑</span>
  78. <input type="number" value="2" min="1" class="num" />
  79. <span class="price">200</span><span class="amount">0</span>
  80. </li>
  81. <li>
  82. <input type="checkbox" onchange="checkItems()" checked />
  83. <span class="content">相机</span>
  84. <input type="number" value="3" min="1" class="num" />
  85. <span class="price">300</span>
  86. <span class="amount">0</span>
  87. </li>
  88. <li>
  89. <span>总计:</span>
  90. <span class="total-num">0</span>
  91. <span class="total-amount">0</span>
  92. </li>
  93. </ul>
  94. <!-- 点击结算按钮时自动计算选中商品 -->
  95. <button class="account" onclick="autoCalculate()">结算</button>
  96. </div>
  97. <script>
  98. function checkAll(){
  99. let status = event.target.checked;
  100. let items = document.querySelectorAll('.list li input[type="checkbox"]');
  101. items.forEach(item => (item.checked = status));
  102. }
  103. function checkItems(){
  104. let items = document.querySelectorAll('.list li input[type="checkbox"]');
  105. let status = [...items].every(item => item.checked === true);
  106. document.querySelector('.check-all').checked = status;
  107. }
  108. // 商品自动计算
  109. // 所有计算都是基于选中商品数量的变化
  110. const nums = document.querySelectorAll('.num');
  111. // 1. 计算总数量
  112. function getTotalNum(numArr) {
  113. return numArr.reduce((acc, cur) => acc + cur);
  114. }
  115. // 2. 计算每个商品的金额
  116. function getAmount(numArr, priceArr) {
  117. // 金额 = 数量 * 单价
  118. return numArr.map((num, index) => num * priceArr[index]);
  119. }
  120. // console.log(getAmount([1, 2, 3], [100, 200, 300]));
  121. // 3. 计算总金额
  122. function getTotalAmount(amountArr) {
  123. return amountArr.reduce((acc, cur) => acc + cur);
  124. }
  125. // 4. 自动计算
  126. function autoCalculate() {
  127. // 选中商品数量数组(以父节点li的第一个子元素input的checked属性判断商品是否选中)
  128. const numArr = [...nums].filter(item => item.parentNode.firstElementChild.checked === true).map(num => parseInt(num.value));
  129. console.log(numArr);
  130. // 选中单价数组
  131. const prices = document.querySelectorAll('.price');
  132. const priceArr = [...prices].filter(item => item.parentNode.firstElementChild.checked === true).map(price => parseInt(price.textContent));
  133. console.log(priceArr);
  134. // 选中金额数组
  135. const amountArr = getAmount(numArr, priceArr);
  136. console.log(amountArr);
  137. // 选中商品总数量
  138. console.log(getTotalNum(numArr));
  139. document.querySelector('.total-num').textContent = getTotalNum(numArr);
  140. // 选中商品总金额
  141. document.querySelector('.total-amount').textContent = getTotalAmount(amountArr);
  142. // 为选中的商品填充金额,未选购的商品备注
  143. const amount = document.querySelectorAll('.amount');
  144. amount.forEach((amount, index) => (amount.textContent = "未选购"));
  145. [...amount].filter(item => item.parentNode.firstElementChild.checked === true).forEach((amount, index) => (amount.textContent = amountArr[index]));
  146. }
  147. // 当页面加载的时候自动计算
  148. // window.onload = autoCalculate;
  149. // 当数量更新时,自动计算所有数据
  150. nums.forEach(num => (num.onchange = autoCalculate));
  151. </script>
  152. </body>
  153. </html>

声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议