Blogger Information
Blog 34
fans 0
comment 0
visits 18333
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
0412作业-购物车
千山暮雪
Original
384 people have browsed it


实现:
1.全选,单选商品时价格数量计算
2.数量变化时自动选择商品及价格数量计算

  • css
  1. table {
  2. border-collapse: collapse;
  3. width: 90%;
  4. text-align: center;
  5. margin: auto;
  6. }
  7. table caption {
  8. margin-bottom: 15px;
  9. font-size: 1.5rem;
  10. }
  11. table th,
  12. table td {
  13. border-bottom: 1px solid #ccc;
  14. padding: 5px;
  15. font-weight: normal;
  16. }
  17. table thead tr:first-of-type {
  18. background-color: #e6e6e6;
  19. height: 3em;
  20. }
  21. table input[type="checkbox"] {
  22. width: 1.5em;
  23. height: 1.5em;
  24. }
  25. table tbody tr {
  26. border-bottom: 1px solid #ccc;
  27. }
  28. table tbody tr:hover {
  29. background-color: #f6f6f6;
  30. cursor: pointer;
  31. }
  32. tbody img {
  33. width: 3em;
  34. }
  35. tbody input[type="number"] {
  36. width: 3em;
  37. }
  38. button {
  39. width: 150px;
  40. height: 30px;
  41. outline: none;
  42. border: none;
  43. background-color: teal;
  44. color: white;
  45. letter-spacing: 5px;
  46. }
  47. button:hover {
  48. opacity: 0.7;
  49. cursor: pointer;
  50. }
  • html
  1. <table>
  2. <caption>
  3. 购物车
  4. </caption>
  5. <thead>
  6. <tr>
  7. <!-- 全选复选框 -->
  8. <th><input type="checkbox" name="checkAll" id="check-all" /><label for="check-all">全选</label>
  9. </th>
  10. <th>图片</th>
  11. <th>品名</th>
  12. <th>单位</th>
  13. <th>单价/元</th>
  14. <th>数量</th>
  15. <th>金额/元</th>
  16. </tr>
  17. </thead>
  18. <tbody>
  19. <tr>
  20. <td>
  21. <input type="checkbox" name="item" value="SN-1020" />
  22. </td>
  23. <td>
  24. <a href=""><img src="images/p1.jpg" alt="" /></a>
  25. </td>
  26. <td>iPhone 11</td>
  27. <td></td>
  28. <td>4799</td>
  29. <td><input type="number" min="1" value="1" /></td>
  30. <td class="amount">0</td>
  31. </tr>
  32. <tr>
  33. <td>
  34. <input type="checkbox" name="item" value="SN-1020" />
  35. </td>
  36. <td>
  37. <a href=""><img src="images/p2.jpg" alt="" /></a>
  38. </td>
  39. <td>小米pro 11</td>
  40. <td></td>
  41. <td>3999</td>
  42. <td><input type="number" min="1" value="2" /></td>
  43. <td class="amount">0</td>
  44. </tr>
  45. <tr>
  46. <td>
  47. <input type="checkbox" name="item" value="SN-1030" />
  48. </td>
  49. <td>
  50. <a href=""><img src="images/p3.jpg" alt="" /></a>
  51. </td>
  52. <td>MacBook Pro</td>
  53. <td></td>
  54. <td>18999</td>
  55. <td><input type="number" min="1" value="1" /></td>
  56. <td class="amount">0</td>
  57. </tr>
  58. <tr>
  59. <td>
  60. <input type="checkbox" name="item" value="SN-1040" />
  61. </td>
  62. <td>
  63. <a href=""><img src="images/p4.jpg" alt="" /></a>
  64. </td>
  65. <td>小米75电视</td>
  66. <td></td>
  67. <td>5999</td>
  68. <td><input type="number" min="1" value="2" /></td>
  69. <td class="amount">0</td>
  70. </tr>
  71. <tr>
  72. <td>
  73. <input type="checkbox" name="item" value="SN-1050" />
  74. </td>
  75. <td>
  76. <a href=""><img src="images/p5.jpg" alt="" /></a>
  77. </td>
  78. <td>Canon 90D单反</td>
  79. <td></td>
  80. <td>9699</td>
  81. <td><input type="number" min="1" value="1" /></td>
  82. <td class="amount">0</td>
  83. </tr>
  84. </tbody>
  85. <tfoot>
  86. <tr style="font-weight: bolder; font-size: 1.2em">
  87. <td colspan="5">已选总计:</td>
  88. <td id="sum">0</td>
  89. <td id="total-amount">0</td>
  90. </tr>
  91. </tfoot>
  92. </table>
  93. <div style="width: 90%; margin: 10px auto">
  94. <button style="float: right; width: 100px">结算</button>
  95. </div>
  • js
  1. // 全选ele
  2. const checkAll = document.querySelector('#check-all');
  3. // 单选商品ele
  4. const checkItems = document.querySelectorAll('input[name="item"]');
  5. // 单商品价格ele
  6. const price = document.querySelectorAll('tr > td:nth-of-type(5)');
  7. // 单商品个数ele
  8. const itemNum = document.querySelectorAll('input[type="number"]');
  9. // 单商品总价ele
  10. const itemAmount = document.querySelectorAll('.amount');
  11. // 全总价ele
  12. const totalAmount = document.querySelector('#total-amount');
  13. // 全商品总数ele
  14. const itemTotal = document.querySelector('#sum');
  15. // 定义数组相乘函数
  16. function resum(arr1, arr2) {
  17. return [arr1, arr2].reduce((pre, cur) => pre.map((item, index) => item * cur[index]));
  18. }
  19. // 所有变化函数
  20. function allChange() {
  21. // 商品是否选择数组
  22. let checkArr = [...checkItems].map((item) => item.checked * 1);
  23. // 商品数量数组
  24. let numberArr = [...itemNum].map((item) => item.value * 1);
  25. // 商品价格数组
  26. let priceArr = [...price].map((item) => item.textContent * 1);
  27. // 单商品总价数组
  28. let itemAmountArr = resum(priceArr, numberArr);
  29. // 单商品总价写入ele
  30. itemAmount.forEach((item, index) => item.textContent = itemAmountArr[index]);
  31. // 选定商品总数写入ele
  32. itemTotal.textContent = resum(numberArr, checkArr).reduce((pre, cur) => pre + cur);
  33. // 选定商品总价写入ele
  34. totalAmount.textContent = resum(itemAmountArr, checkArr).reduce((pre, cur) => pre + cur);
  35. }
  36. // 全选事件
  37. checkAll.onchange = ((ev) => {
  38. checkItems.forEach((item) => { item.checked = checkAll.checked });
  39. allChange();
  40. });
  41. // 单选事件
  42. checkItems.forEach((item) => {
  43. item.onchange = (ev) => {
  44. checkAll.checked = [...checkItems].every((item) => item.checked);
  45. allChange();
  46. }
  47. })
  48. // 数量变化事件
  49. itemNum.forEach((item, index) => {
  50. item.onchange = (ev) => {
  51. // 自动选择
  52. checkItems[index].checked = true;
  53. allChange();
  54. };
  55. })
  56. // 加载时
  57. window.onload = (() => allChange());
Correcting teacher:天蓬老师天蓬老师

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