ASCII码 ASCII码

购物车的选择计算功能

发布于:2022-01-19 10:29:46  栏目:技术文档

购物车的计算功能(没有考虑不勾选物品)

  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>Document</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: 3em 3em 3em 3em 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: 5em;
  51. height: 2em;
  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. <input
  63. type="checkbox"
  64. class="check-all"
  65. name="check-all"
  66. id="check-all"
  67. onchange="checkAll()"
  68. checked
  69. />
  70. <label for="check-all">全选</label>
  71. </div>
  72. <ul class="list">
  73. <li>
  74. <span>选择</span> <span>品名</span> <span>数量</span>
  75. <span>单价</span>
  76. <span>金额</span>
  77. </li>
  78. <li>
  79. <input type="checkbox" onchange="checkItems()" checked />
  80. <span class="content">手机</span>
  81. <input type="number" value="1" min="1" class="num" />
  82. <span class="price">100</span>
  83. <span class="amount">0</span>
  84. </li>
  85. <li>
  86. <input type="checkbox" onchange="checkItems()" />
  87. <span class="content">电脑</span>
  88. <input type="number" value="2" min="1" class="num" />
  89. <span class="price">200</span>
  90. <span class="amount">0</span>
  91. </li>
  92. <li>
  93. <input type="checkbox" onchange="checkItems()" checked />
  94. <span class="content">相机</span>
  95. <input type="number" value="3" min="1" class="num" />
  96. <span class="price">300</span>
  97. <span class="amount">0</span>
  98. </li>
  99. <li>
  100. <span>总计:</span>
  101. <span class="total-num">0</span>
  102. <span class="total-amount">0</span>
  103. </li>
  104. </ul>
  105. <button class="account">结算</button>
  106. </div>
  107. <script>
  108. function checkAll() {
  109. // console.log(event.target.checked);
  110. let status = event.target.checked;
  111. const items = document.querySelectorAll(
  112. '.list li input[type="checkbox'
  113. );
  114. // console.log(items);
  115. items.forEach((item) => (item.checked = status));
  116. }
  117. function checkItems() {
  118. const items = document.querySelectorAll(
  119. '.list li input[type="checkbox"]'
  120. );
  121. let status = [...items].every((item) => item.checked === true);
  122. document.querySelector(".check-all").checked = status;
  123. }
  124. const nums = document.querySelectorAll(".num");
  125. // console.log(nums);
  126. function getTotalNum(numArr) {
  127. // console.log("---" + numArr);
  128. return numArr.reduce((acc, cur) => acc + cur);
  129. }
  130. // console.log(getTotalNum([1, 2, 3]));
  131. function getAmount(numArr, priceArr) {
  132. return numArr.map((num, index) => num * priceArr[index]);
  133. }
  134. // console.log(getAmount([1, 2, 3], [100, 200, 300]));
  135. function getTotalAmount(amountArr) {
  136. return amountArr.reduce((acc, cur) => acc + cur);
  137. }
  138. // console.log(getTotalAmount([100, 400, 900]));
  139. function autoCalculate() {
  140. const numArr = [...nums].map((num) => Number(num.value)); //parseInt
  141. // console.log(numArr);
  142. const prices = document.querySelectorAll(".price");
  143. const priceArr = [...prices].map((price) =>
  144. parseInt(price.textContent)
  145. );
  146. // console.log(priceArr);
  147. const amountArr = getAmount(numArr, priceArr);
  148. console.log(amountArr);
  149. document.querySelector(".total-num").textContent = getTotalNum(numArr);
  150. document
  151. .querySelectorAll(".amount")
  152. .forEach((amount, index) => (amount.textContent = amountArr[index]));
  153. document.querySelector(".total-amount").textContent =
  154. getTotalAmount(amountArr);
  155. }
  156. // console.log(autoCalculate());
  157. window.onload = autoCalculate();
  158. nums.forEach((num) => (num.onchange = autoCalculate));
  159. </script>
  160. </body>
  161. </html>

购物车的选择计算功能

思路:不勾选的物品不考虑计算,要多一步拿到所有勾选的物品。除此之外,ul中的第一和最后一个li是不考虑的。

  1. const allLi = document.querySelectorAll(
  2. ".list li:not(:first-of-type,:last-of-type)" );

其他的实现有一些比较细节的东西,在源码中体现。

  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>Document</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: 3em 3em 3em 3em 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: 5em;
  51. height: 2em;
  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. <input
  63. type="checkbox"
  64. class="check-all"
  65. name="check-all"
  66. id="check-all"
  67. onchange="checkAll(),autoCalculate()"
  68. checked
  69. />
  70. <label for="check-all">全选</label>
  71. </div>
  72. <ul class="list">
  73. <li>
  74. <span>选择</span> <span>品名</span> <span>数量</span>
  75. <span>单价</span>
  76. <span>金额</span>
  77. </li>
  78. <li>
  79. <input
  80. type="checkbox"
  81. onchange="checkItems(),autoCalculate()"
  82. checked
  83. />
  84. <span class="content">手机</span>
  85. <input type="number" value="1" min="1" class="num" />
  86. <span class="price">100</span>
  87. <span class="amount">0</span>
  88. </li>
  89. <li>
  90. <input type="checkbox" onchange="checkItems(),autoCalculate()" />
  91. <span class="content">电脑</span>
  92. <input type="number" value="2" min="1" class="num" />
  93. <span class="price">200</span>
  94. <span class="amount">0</span>
  95. </li>
  96. <li>
  97. <input
  98. type="checkbox"
  99. onchange="checkItems(),autoCalculate()"
  100. checked
  101. />
  102. <span class="content">相机</span>
  103. <input type="number" value="3" min="1" class="num" />
  104. <span class="price">300</span>
  105. <span class="amount">0</span>
  106. </li>
  107. <li>
  108. <span>总计:</span>
  109. <span class="total-num">0</span>
  110. <span class="total-amount">0</span>
  111. </li>
  112. </ul>
  113. <button class="account">结算</button>
  114. </div>
  115. <script>
  116. function checkAll() {
  117. // console.log(event.target.checked);
  118. let status = event.target.checked;
  119. const items = document.querySelectorAll(
  120. '.list li input[type="checkbox'
  121. );
  122. // console.log(items);
  123. items.forEach((item) => (item.checked = status));
  124. }
  125. function checkItems() {
  126. const items = document.querySelectorAll(
  127. '.list li input[type="checkbox"]'
  128. );
  129. let status = [...items].every((item) => item.checked === true);
  130. document.querySelector(".check-all").checked = status;
  131. }
  132. //const nums = document.querySelectorAll(".num");
  133. // console.log(nums);
  134. ///////////////////
  135. // let nums = [];
  136. // let prices = [];
  137. // const allLi = document.querySelectorAll(
  138. // ".list li:not(:first-of-type,:last-of-type)"
  139. // );
  140. // console.log(allLi);
  141. // allLi.forEach((li, index) => {
  142. // // console.log(li.querySelector("input:first-of-type").checked, index);
  143. // if (li.querySelector("input:first-of-type").checked === true) {
  144. // nums.push(Number(li.querySelector(".num").value));
  145. // prices.push(Number(li.querySelector(".price").textContent));
  146. // }
  147. // });
  148. // console.log("---" + nums + prices);
  149. //////////////////
  150. function getTotalNum(numArr) {
  151. // console.log("---" + numArr);
  152. return numArr.reduce((acc, cur) => acc + cur);
  153. }
  154. // console.log(getTotalNum([1, 2, 3]));
  155. function getAmount(numArr, priceArr) {
  156. return numArr.map((num, index) => num * priceArr[index]);
  157. }
  158. // console.log(getAmount([1, 2, 3], [100, 200, 300]));
  159. function getTotalAmount(amountArr) {
  160. return amountArr.reduce((acc, cur) => acc + cur);
  161. }
  162. // console.log(getTotalAmount([100, 400, 900]));
  163. function autoCalculate() {
  164. let nums = [];
  165. let prices = [];
  166. const allLi = document.querySelectorAll(
  167. ".list li:not(:first-of-type,:last-of-type)"
  168. );
  169. console.log(allLi);
  170. allLi.forEach((li, index) => {
  171. // console.log(li.querySelector("input:first-of-type").checked, index);
  172. if (li.querySelector("input:first-of-type").checked === true) {
  173. nums.push(Number(li.querySelector(".num").value));
  174. prices.push(Number(li.querySelector(".price").textContent));
  175. }
  176. });
  177. const numArr = nums;
  178. // console.log(numArr);
  179. const priceArr = prices;
  180. // console.log(priceArr);
  181. const amountArr = getAmount(numArr, priceArr);
  182. document.querySelector(".total-num").textContent = getTotalNum(numArr);
  183. // document.querySelectorAll(".amount").forEach((amount, index) => (amount.textContent = amountArr[index]));
  184. let index2 = 0;
  185. document.querySelectorAll(".amount").forEach((amount) => {
  186. if (
  187. amount.parentElement.querySelector("input:first-of-type")
  188. .checked === true
  189. ) {
  190. amount.textContent = amountArr[index2];
  191. index2 = index2 + 1;
  192. }
  193. });
  194. document.querySelector(".total-amount").textContent =
  195. getTotalAmount(amountArr);
  196. }
  197. // console.log(autoCalculate());
  198. window.onload = autoCalculate();
  199. const allNum = document.querySelectorAll(".num");
  200. [...allNum].forEach((num) => (num.onchange = autoCalculate));
  201. </script>
  202. </body>
  203. </html>

相关推荐
阅读 +