ASCII码 ASCII码

计算-金额

发布于:2022-04-10 12:43:09  栏目:技术文档
  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. <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. <input type="checkbox" onchange="checkItems()" checked />
  90. <span class="content">游戏机</span>
  91. <input type="number" value="2" min="1" class="num" />
  92. <span class="price">3000</span>
  93. <span class="amount">0</span>
  94. </li>
  95. <li>
  96. <span>总计:</span>
  97. <span class="total-num">0</span>
  98. <span class="total-amount">0</span>
  99. </li>
  100. </ul>
  101. <button class="account">结算</button>
  102. </div>
  103. <script>
  104. var items = document.querySelectorAll('.list li input[type="checkbox"]');
  105. // 1. 全选
  106. function checkAll() {
  107. // 1. 全选按钮状态
  108. let status = event.target.checked;
  109. // console.log(status);
  110. // 2. 根据状态动态设置所有商品的状态
  111. items.forEach(item => (item.checked = status));
  112. autoCalculate();
  113. // document
  114. // .querySelectorAll('.list li input[type="checkbox"]')
  115. // .forEach(item => (item.checked = event.target.checked));
  116. }
  117. // 2. 根据用户选择来动态设置全选状态
  118. function checkItems() {
  119. // 1. 拿到全部商品
  120. // let items = document.querySelectorAll('.list li input[type="checkbox"]');
  121. // 2. 判断状态,只有全部被选择,才需要设置全选为true, array.every
  122. let status = [...items].every(item => item.checked === true);
  123. // console.log(status);
  124. // 3. 将这个新状态设置到全选按钮
  125. document.querySelector('.check-all').checked = status;
  126. autoCalculate();
  127. // document.querySelector('.check-all').checked = [
  128. // ...document.querySelectorAll('.list li input[type="checkbox"]'),
  129. // ].every(item => item.checked === true);
  130. }
  131. // 商品自动计算
  132. // 所有计算都是基于商品数量的变化
  133. // nums.forEach(item => console.log(item.value));
  134. // 1. 计算总数量
  135. function getTotalNum(numArr,index) {
  136. return numArr.reduce((acc, cur) => acc + cur);
  137. }
  138. // 2. 计算每个商品的金额
  139. function getAmount(numArr, priceArr) {
  140. // 金额 = 数量 * 单价
  141. return numArr.map((num, index) => num * priceArr[index]);
  142. }
  143. // console.log(getAmount([1, 2, 3], [100, 200, 300]));
  144. // 3. 计算总金额
  145. function getTotalAmount(amountArr,index) {
  146. return amountArr.reduce((acc, cur) => acc + cur);
  147. }
  148. // 数量
  149. const nums = document.querySelectorAll('.num');
  150. // 单价数组
  151. const prices = document.querySelectorAll('.price');
  152. // 4. 自动计算
  153. function autoCalculate() {
  154. // 商品数量数组
  155. const numArr = [...nums].map(num => parseInt(num.value));
  156. console.log(numArr);
  157. // 单价数组
  158. const priceArr = [...prices].map(price => parseInt(price.textContent));
  159. console.log(priceArr);
  160. // 金额数组
  161. const amountArr = getAmount(numArr, priceArr);
  162. // 循环判断是否选中 没有选中的话直接赋值为0
  163. items.forEach(function(val,index){
  164. if(val.checked === false){
  165. numArr[index] = 0;
  166. priceArr[index] = 0;
  167. }
  168. });
  169. console.log(numArr);
  170. // 未来不影响单个金额计算 重新写计算一次单个金额
  171. const amountArr2 = getAmount(numArr, priceArr);
  172. // 总数量
  173. console.log(getTotalNum(numArr));
  174. document.querySelector('.total-num').textContent = getTotalNum(numArr);
  175. // 总金额
  176. document.querySelector('.total-amount').textContent = getTotalAmount(amountArr2);
  177. // 为每个商品填充金额
  178. document.querySelectorAll('.amount').forEach((amount, index) => (amount.textContent = amountArr[index]));
  179. }
  180. // 当页面加载的时候自动计算
  181. window.onload = autoCalculate;
  182. // 当数量更新时,自动计算所有数据
  183. nums.forEach(num => (num.onchange = autoCalculate));
  184. // 作业 : 只计算选中商品的金额,同时更新总数量和总金额
  185. // vue: 计算属性, 侦听器,可以极大的简化以上过程
  186. </script>
  187. </body>
  188. </html>
相关推荐
阅读 +