0
点赞
收藏
分享

微信扫一扫

JavaScript pc购物车 Css jQuery---实现

技术只适用于干活 2022-02-28 阅读 20
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <link rel="stylesheet" href="../bootstrap-3.4.1-dist/bootstrap-3.4.1-dist/css/bootstrap-theme.min.css">
    <style>
        img {
            width: 100px;
            height: 100px;
            float: left;
        }
        .red {
            background-color: red;
        }
        .pink {
            background-color: pink;
        }
    </style>
  </head>
  <body>
    <div class="container">
      <table class="table">
          <thead>
              <tr>
                  <th><input type="checkbox" class="ckAll">全选</th>
                  <th>商品</th>
                  <th>单价</th>
                  <th>数量</th>
                  <th>小计</th>
                  <th>操作</th>
              </tr>
          </thead>
          <tbody>
            <tr>
                <td><input type="checkbox" class="ck"></td>
                <td>
                    <img src="../images/01.jpg" alt="">
                    <p>经典儿童文学</p>
                </td>
                <td class="dj">2</td>
                <td>
                    <button class="sub">-</button>
                    <input type="text" value="1" class="txt">
                    <button class="add">+</button>
                </td>
                <td class="xj">2</td>
                <td class="del">删除</td>
            </tr>
            <tr>
                <td><input type="checkbox" class="ck"></td>
                <td>
                    <img src="../images/02.jpg" alt="">
                    <p>经典儿童文学</p>
                </td>
                <td class="dj">3</td>
                <td>
                    <button class="sub">-</button>
                    <input type="text" value="1" class="txt">
                    <button class="add">+</button>
                </td>
                <td class="xj">3</td>
                <td class="del">删除</td>
            </tr>
            <tr>
                <td><input type="checkbox" class="ck"></td>
                <td>
                    <img src="../images/03.jpg" alt="">
                    <p>经典儿童文学</p>
                </td>
                <td class="dj">4</td>
                <td>
                    <button class="sub">-</button>
                    <input type="text" value="1" class="txt">
                    <button class="add">+</button>
                </td>
                <td class="xj">4</td>
                <td class="del">删除</td>
            </tr>
        </tbody>
      </table>
      <p>
          <span class="delS">删除选中的商品</span>
          <span class="qk">清空购物车</span>
          已选择<span class="sp1">0</span>件商品,总价:<span class="sp2">0</span>
          <button type="button" class="btn buy">去结算</button>
      </p>
    </div>
  </body>
  <script src="../jquery.min.js"></script>
  <script src="./index.js"></script>
</html>
$(function () {
  // 1、全选的功能   将父的选中状态赋值给子
  $(".ckAll").click(function () {
    // 1.1 获取父的选中状态
    var flag = $(this).prop("checked");
    // 1.2 将父的状态设置给子
    $(".ck").prop("checked", flag);
    // 实现商品选中,添加背景色
    if(flag == true){
      $("tbody tr").addClass("pink")
    }else {
      $("tbody tr").removeClass("pink")
    }
    getSum();
  });
  // 2、全不选    选中的个数是否等于总个数
  $(".ck").click(function () {
    var num = $(".ck:checked").length; // 选中的个数
    var len = $(".ck").length; // 总个数

    if (num == len) {
      $(".ckAll").prop("checked", true);
    } else {
      $(".ckAll").prop("checked", false);
    }
    getSum();
    // 实现商品选中,添加背景色
    var flag = $(this).prop("checked");
    if (flag == true) {
      $(this).parents("tr").addClass("pink")
    }else {
      $(this).parents("tr").removeClass("pink")
    }
  });

  // 3、加
  $(".add").click(function () {
    // 3.1 取
    var a = $(this).siblings(".txt").val();
    // 3.2 自加1
    a++;
    // 3.3 放回去
    $(this).siblings(".txt").val(a);
    // 3.4 解除减号按钮的禁用
    $(this).siblings(".sub").prop("disabled", false);
    // 3.5 小计 = 单价 * 数量
    var b = $(this).parent().siblings(".dj").text();
    var c = b * a;
    $(this).parent().siblings(".xj").text(c);
    getSum();
  });
  // 4、减
  $(".sub").click(function () {
    var a = $(this).siblings(".txt").val();
    if (a <= 1) {
      // 禁用按钮
      $(this).prop("disabled", true);
      return false;
    }
    a--;
    $(this).siblings(".txt").val(a);
    // 小计
    var b = $(this).parent().siblings(".dj").text();
    var c = b * a;
    $(this).parent().siblings(".xj").text(c);
    getSum();
  });
  // 5、删除当前
  $(".del").click(function () {
    $(this).parent().remove();
    getSum();
  });
  // 6、删除选中
  $(".delS").click(function () {
    // console.log($(".ck:checked"));
    $(".ck:checked").parents("tr").remove();
    getSum();
  });

  // 7、清空购物车
  $(".qk").click(function () {
    $("tbody").html("");
    getSum();
  });

  // 8、封装一个计算总价的函数
  console.log($(".ck"));
  console.log($(".txt"));
  console.log($(".xj"));
  function getSum() {
    var sum1 = 0; //总个数    
    var sum2 = 0; //总价钱
    // index 0 1 2
    $(".ck").each(function (index, ele) {
      if ($(ele).prop("checked") == true) {
        sum1 += parseInt($(".txt").eq(index).val());
        sum2 += parseInt($(".xj").eq(index).text());
      }
    });
    $(".sp1").text(sum1);
    $(".sp2").text(sum2);
    // 判断去结算的高亮状态
    if (sum1 > 0) {
      $(".buy").addClass("red");
    } else {
      $(".buy").removeClass("red");
    }
  }

  getSum();
});
举报

相关推荐

0 条评论