ASCII码 ASCII码

javascript实现购物车(完整功能)

发布于:2022-04-17 13:59:07  栏目:技术文档

1)html部分

  1. <div class="box">
  2. <h3>我的购物车</h3>
  3. <div class="selectAll">
  4. <input type="checkbox" class="check-all" name="check-all" onchange="checkAll()" checked />
  5. <label for="check-all">全选</label>
  6. </div>
  7. <ul class="list">
  8. <li><span>选择</span><span>品名</span><span>数量</span><span>单价</span><span>金额</span></li>
  9. <li>
  10. <input type="checkbox" onchange="checkItems()" checked />
  11. <span class="content">手机</span>
  12. <input type="number" value="1" min="1" class="num" />
  13. <span class="price">100</span>
  14. <span class="amount">0</span>
  15. </li>
  16. <li>
  17. <input type="checkbox" onchange="checkItems()" checked />
  18. <span class="content">电脑</span>
  19. <input type="number" value="2" min="1" class="num" />
  20. <span class="price">200</span>
  21. <span class="amount">0</span>
  22. </li>
  23. <li>
  24. <input type="checkbox" onchange="checkItems()" checked />
  25. <span class="content">相机</span>
  26. <input type="number" value="3" min="1" class="num" />
  27. <span class="price">300</span>
  28. <span class="amount">0</span>
  29. </li>
  30. <li>
  31. <span>总计:</span>
  32. <span class="total-num">0</span>
  33. <span class="total-amount">0</span>
  34. </li>
  35. </ul>
  36. <button class="account">结算</button>
  37. </div>

2)css部分

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

3)javascript部分

  1. // 1. 全选
  2. function checkAll() {
  3. let status = event.target.checked;
  4. let items = document.querySelectorAll('.list li input[type="checkbox"]');
  5. items.forEach(item => (item.checked = status));
  6. autoCalculate();//增加项
  7. }
  8. // 2. 根据用户选择来动态设置全选状态
  9. function checkItems() {
  10. let items = document.querySelectorAll('.list li input[type="checkbox"]');
  11. let status = [...items].every(item => item.checked === true);
  12. document.querySelector('.check-all').checked = status;
  13. autoCalculate();//增加项
  14. }
  15. // 所有计算都是基于商品数量的变化
  16. const nums = document.querySelectorAll('.num');
  17. // 计算总数量
  18. function getTotalNum(numArr) {
  19. return numArr.reduce((acc, cur) => acc + cur,0);//修改项
  20. }
  21. // 计算每个商品的金额
  22. function getAmount(numArr, priceArr) {
  23. return numArr.map((num, index) => num * priceArr[index]);
  24. }
  25. // 计算总金额
  26. function getTotalAmount(amountArr) {
  27. return amountArr.reduce((acc, cur) => acc + cur,0);//修改项
  28. }
  29. // 自动计算
  30. function autoCalculate() {
  31. const numArr = [...nums].map(num => parseInt(num.value));
  32. const prices = document.querySelectorAll('.price');
  33. const priceArr = [...prices].map(price => parseInt(price.textContent));
  34. const amountArr = getAmount(numArr, priceArr);
  35. document.querySelectorAll('.amount').forEach((amount, index) => (amount.textContent = amountArr[index]));
  36. //统计被选项数据,以下均为添加项
  37. let items = document.querySelectorAll('.list li input[type="checkbox"]');
  38. let cbArr=[...items].map(item=>item.checked);
  39. let numArr1 =numArr.filter((item,index)=>cbArr[index]==true);
  40. let priceArr1=priceArr.filter((item,index)=>cbArr[index]==true);
  41. let amountArr1 = getAmount(numArr1, priceArr1);
  42. //填充实际数量
  43. document.querySelector('.total-num').textContent = getTotalNum(numArr1);
  44. //填充实际金额
  45. document.querySelector('.total-amount').textContent = getTotalAmount(amountArr1);
  46. }
  47. // 当页面加载的时候自动计算
  48. window.onload = autoCalculate;
  49. // 当数量更新时,自动计算所有数据
  50. nums.forEach(num => (num.onchange = autoCalculate));
相关推荐
阅读 +