Blogger Information
Blog 35
fans 0
comment 0
visits 17157
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
0729-购物车实例和JSON数据接口
三九三伏
Original
479 people have browsed it

一、购物车选择或不选商品时,并自动过滤未选商品,自动计算

思路:
1、在勾选和取消商品时触发重新计算
2、在计算总金额时判断商品勾选的状态
3、在计算总数量时判断商品勾选的状态
4、在全选时触发重新计算
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>Document</title>
  8. <link rel="stylesheet" href="./static/css/reset.css">
  9. <link rel="stylesheet" href="test.css">
  10. </head>
  11. <body>
  12. <div class="box">
  13. <!-- 全选按钮 -->
  14. <div class="selectall">
  15. <input type="checkbox" class="check-all" name="check-all" onchange="checkAll()" checked />
  16. <label for="check-all">全选</label>
  17. </div>
  18. <!-- 商品列表 -->
  19. <ul class="list">
  20. <li><span>选择</span><span>品名</span><span>数量</span><span>单价</span><span>金额</span></li>
  21. <li>
  22. <input type="checkbox" onchange="checkItems()" checked />
  23. <span class="content">手机</span>
  24. <input type="number" value="1" min="1" class="num" />
  25. <span class="price">100</span>
  26. <span class="amount">0</span>
  27. </li>
  28. <li>
  29. <input type="checkbox" onchange="checkItems()" checked />
  30. <span class="content">电脑</span>
  31. <input type="number" value="2" min="1" class="num" />
  32. <span class="price">200</span>
  33. <span class="amount">0</span>
  34. </li>
  35. <li>
  36. <input type="checkbox" onchange="checkItems()" checked />
  37. <span class="content">相机</span>
  38. <input type="number" value="3" min="1" class="num" />
  39. <span class="price">300</span>
  40. <span class="amount">0</span>
  41. </li>
  42. <li>
  43. <span>总计:</span>
  44. <span class="total-num">0</span>
  45. <span class="total-amount">0</span>
  46. </li>
  47. </ul>
  48. <!-- 结算按钮 -->
  49. <button class="account">结算</button>
  50. </div>
  51. <script>
  52. // 全选事件函数
  53. function checkAll(){
  54. // 获取当前按钮的状态
  55. let status = event.target.checked;
  56. // 根据当前状态,动态设置其他所有商品的选中状态。
  57. const items = document.querySelectorAll('.list li input[type="checkbox"]');
  58. items.forEach(items => (items.checked = status));
  59. // 在全选时触发重新计算
  60. autoCalculate();
  61. }
  62. // 单个商品的选择与否对全选的影响函数
  63. function checkItems(){
  64. // 获取所有的商品
  65. const items = document.querySelectorAll('.list li input[type="checkbox"]');
  66. // 判断当前状态,只有全部选中,才返回true,否则返回false。
  67. let status = [...items].every(item => item.checked === true);
  68. //根据状态更新全选按钮
  69. document.querySelector('.check-all').checked = status;
  70. // 在勾选和取消商品时触发重新计算
  71. autoCalculate();
  72. }
  73. // 获取所有商品数量的元素
  74. const nums = document.querySelectorAll('.list .num');
  75. // 计算商品总数
  76. function getTotalNum(numArr){
  77. const items = document.querySelectorAll('.list li input[type="checkbox"]');
  78. let item = [...items].map(item => item.checked);
  79. let tn = 0;
  80. for(let i= 0; i < numArr.length; i++){
  81. // console.log(i);
  82. // 在计算总数量时判断商品勾选的状态
  83. if(item[i] == true)
  84. tn = tn + numArr[i];
  85. }
  86. return tn;
  87. }
  88. let numArr = ([...nums].map(item => parseInt(item.value)));
  89. // 计算每个商品金额:数量*单价,数量数组numArr,单价数组priceArr。
  90. function getAmount(numArr, priceArr){
  91. return numArr.map((num, index) => {
  92. // console.log(num, index);
  93. // index用来回去单价
  94. return num * priceArr[index];
  95. });
  96. }
  97. // console.log(getAmount([1, 2, 3],[100, 200, 300]));
  98. // 计算总金额
  99. function getTotalAmount(amountArr){
  100. const items = document.querySelectorAll('.list li input[type="checkbox"]');
  101. let item = [...items].map(item => item.checked);
  102. let ta = 0;
  103. for(let i= 0; i < amountArr.length; i++){
  104. // console.log(i);
  105. // 在计算总金额时判断商品勾选的状态
  106. if(item[i] == true)
  107. ta = ta + amountArr[i];
  108. }
  109. return ta;
  110. }
  111. // console.log(getTotalAmount(getAmount([1, 2, 3],[100, 200, 300])));
  112. // 自动计算的通用方法
  113. function autoCalculate(){
  114. // 数量数组
  115. let numArr = [...nums].map(item => parseInt(item.value));
  116. // 单价数组
  117. const prices = document.querySelectorAll('.list .price');
  118. let priceArr = [...prices].map(item => item.textContent);
  119. // console.log(priceArr);
  120. // 金额数组
  121. const amountArr = getAmount(numArr, priceArr);
  122. // console.log(amountArr);
  123. // 将数据添加到页面中显示
  124. // 总数量
  125. document.querySelector('.total-num').textContent = getTotalNum(numArr);
  126. // 每个商品的金额
  127. document.querySelectorAll('.amount').forEach((item, index) => (item.textContent = amountArr[index]));
  128. // 总金额
  129. document.querySelector('.total-amount').textContent = getTotalAmount(amountArr);
  130. }
  131. // 当页面加载时根据默认值自动计算
  132. window.addEventListener('load', autoCalculate, false);
  133. // 当用户更新商品数量时,将会触发商品金额,总数量的动态更新。
  134. nums.forEach(item => (item.onchange = autoCalculate));
  135. </script>
  136. </body>
  137. </html>

CSS部分代码:

  1. .box {
  2. width: 22em;
  3. height: 2em;
  4. /* background-color: aqua; */
  5. }
  6. .list > li {
  7. height: 1.6em;
  8. background-color: #efefef;
  9. display: grid;
  10. grid-template-columns: repeat(5, 3em);
  11. gap: 1em;
  12. place-items: center right;
  13. border-bottom: 1px solid #ccc;
  14. }
  15. .list > li:first-of-type {
  16. background-color: lightseagreen;
  17. color: white;
  18. }
  19. .list > li:hover:not(:first-of-type) {
  20. cursor: pointer;
  21. background-color: lightcyan;
  22. }
  23. .list > li input[type='number'] {
  24. width: 3em;
  25. border: none;
  26. outline: none;
  27. text-align: center;
  28. font-size: 1em;
  29. background-color: transparent;
  30. }
  31. .list > li:last-of-type span.total-num,
  32. .list > li:last-of-type span.total-amount {
  33. grid-column: span 2;
  34. place-self: center right;
  35. color: lightseagreen;
  36. }
  37. .account {
  38. float: right;
  39. background-color: lightseagreen;
  40. color: white;
  41. border: none;
  42. outline: none;
  43. width: 4.5em;
  44. height: 1.8em;
  45. }
  46. .account:hover {
  47. background-color: coral;
  48. cursor: pointer;
  49. }

效果如下:

当取消手机后,

重新全选后,

取消全选后,

二、实例演示JSON常用的二个API, 并详细描述前后端交互的原理与JSON的优势

json本质就是字符串,是格式化的字符串。优势在于简单通用,各种语言可以把他作为中介来传递数据。
常用的两个API
前端到后端:JSON.stringify()
后端到前端:JSON.parse()

前端到后端,js对象到json数据。

  1. <script>
  2. const json_obj = {
  3. id: 1,
  4. sex: 'man',
  5. name:'json',
  6. isMarried: false,
  7. }
  8. console.log(JSON.stringify(json_obj));
  9. </script>

效果:

后端到前端,json数据到js对象

  1. <script>
  2. let str = `{
  3. "id": 1,
  4. "sex": "man",
  5. "name":"json",
  6. "isMarried": false
  7. }`;
  8. console.log(JSON.parse(str));
  9. </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