Blogger Information
Blog 19
fans 0
comment 0
visits 9863
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
购物车单选计算
newbie
Original
590 people have browsed it

购物车单选判断


1.图片效果

2.代码

  1. <script>
  2. // 1. 全选
  3. function checkAll() {
  4. // 1. 全选按钮状态
  5. let status = event.target.checked;
  6. // console.log(status);
  7. // 2. 根据状态动态设置所有商品的状态
  8. let items = document.querySelectorAll(
  9. '.list li input[type="checkbox"]'
  10. );
  11. items.forEach((item) => (item.checked = status));
  12. setTimeout(autoCalculate, 100);
  13. }
  14. // 2. 根据用户选择来动态设置全选状态
  15. function checkItems() {
  16. // 1. 拿到全部商品
  17. let items = document.querySelectorAll(
  18. '.list li input[type="checkbox"]'
  19. );
  20. setTimeout(autoCalculate, 100);
  21. // 2. 判断状态,只有全部被选择,才需要设置全选为true, array.every
  22. let status = [...items].every((item) => item.checked === true);
  23. document.querySelector(".check-all").checked = status;
  24. }
  25. let items = document.querySelectorAll('.list li input[type="checkbox"]');
  26. function getChoice(numArr) {
  27. return numArr.map((num, index) => {
  28. //判断商品是否被选中 选中的话返回正常数组 否则返回0
  29. if (items[index].checked === false) {
  30. return (num = 0);
  31. } else {
  32. return num;
  33. }
  34. });
  35. }
  36. const nums = document.querySelectorAll(".num");
  37. // 1. 计算总数量
  38. function getTotalNum(numArr) {
  39. //将参数传到getChoice函数里面进行判断 再返回值
  40. numArr = getChoice(numArr);
  41. return numArr.reduce((acc, cur) => acc + cur);
  42. }
  43. // 2. 计算每个商品的金额
  44. function getAmount(numArr, priceArr) {
  45. // 金额 = 数量 * 单价
  46. return numArr.map((num, index) => num * priceArr[index]);
  47. }
  48. // 3. 计算总金额
  49. function getTotalAmount(amountArr) {
  50. //将参数传到getChoice函数里面进行判断 再返回值
  51. amountArr = getChoice(amountArr);
  52. return amountArr.reduce((acc, cur) => acc + cur);
  53. }
  54. // 4. 自动计算
  55. function autoCalculate() {
  56. // 商品数量数组
  57. const numArr = [...nums].map((num) => parseInt(num.value));
  58. // 单价数组
  59. const prices = document.querySelectorAll(".price");
  60. const priceArr = [...prices].map((price) =>
  61. parseInt(price.textContent)
  62. );
  63. // 金额数组
  64. const amountArr = getAmount(numArr, priceArr);
  65. // 总数量
  66. console.log(getTotalNum(numArr));
  67. document.querySelector(".total-num").textContent = getTotalNum(numArr);
  68. // 总金额
  69. document.querySelector(".total-amount").textContent =
  70. getTotalAmount(amountArr);
  71. // 为每个商品填充金额
  72. document
  73. .querySelectorAll(".amount")
  74. .forEach((amount, index) => (amount.textContent = amountArr[index]));
  75. }
  76. // 当页面加载的时候自动计算
  77. window.onload = autoCalculate;
  78. // 当数量更新时,自动计算所有数据
  79. nums.forEach((num) => (num.onchange = autoCalculate));
  80. // 作业 : 只计算选中商品的金额,同时更新总数量和总金额
  81. </script>
Correcting teacher:PHPzPHPz

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