jquery-防多店鋪購物車結算全選,單選,及刪除,價格計算


<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>購物車結算</title>
    <style>
        *{padding::0; margin:0; font-family:"微軟雅黑"}
        .one-shop,.all-total{padding:20px; width:400px; margin:auto}
        .one-goods{height:35px; line-height:35px}
        .goods-msg,.goods-price{float:left; padding:0 10px 0 0}
        .goods-check{width:15px; height:15px}
        .am-num-text{width:50px; text-align:center}
        .shop-total,.all-total{height:50px; line-height:50px}
        .all-del{
            width: 400px;
            display: flex;
            margin: auto;
        }
        #AllDel{
            display: inline-block;
            background: #FF0000;
            color: #FFFFFF;
            width: 60px;
            height: 30px;
            line-height: 30px;
            text-align: center;
        }
    </style>
</head>
<body>
    <!-- 一個店鋪 -->
    <div class="one-shop">
        <!-- 一個商品 -->
        <div class="one-goods">
            <div class="goods-msg">
                <input type="checkbox" class="goods-check GoodsCheck">
                <button type="button" class="minus">-</button>
                <input type="text" class="am-num-text" value="1"/>
                <button type="button" class="plus">+</button>
            </div>
            <div class="goods-price">
                單價:¥<span class="shop-total-amount GoodsPrice">20.00</span>
            </div>
        </div>
        <!-- 一個商品 -->
        <div class="one-goods">
            <div class="goods-msg">
                <input type="checkbox" class="goods-check GoodsCheck">
                <button type="button" class="minus">-</button>
                <input type="text" class="am-num-text" value="1"/>
                <button type="button" class="plus">+</button>
            </div>
            <div class="goods-price">
                單價:¥<span class="shop-total-amount GoodsPrice">9.90</span>
            </div>
        </div>
        <!-- 一個商品 -->
        <div class="one-goods">
            <div class="goods-msg ">
                <input type="checkbox" class="goods-check GoodsCheck">
                <button type="button" class="minus">-</button>
                <input type="text" class="am-num-text" value="1"/>
                <button type="button" class="plus">+</button>
            </div>
            <div class="goods-price">
                單價:¥<span class="shop-total-amount GoodsPrice">10.00</span>
            </div>
        </div>
        <!-- 店鋪合計 -->
        <div class="shop-total">
            <input type="checkbox" class="goods-check ShopCheck">店鋪全選&nbsp;&nbsp;&nbsp;&nbsp;
            本店合計:¥<span class="shop-total-amount ShopTotal">0</span>
        </div>
    </div>
    <!-- 一個店鋪 -->
    <div class="one-shop">
        <!-- 一個商品 -->
        <div class="one-goods">
            <div class="goods-msg">
                <input type="checkbox" class="goods-check GoodsCheck">
                <button type="button" class="minus">-</button>
                <input type="text" class="am-num-text" value="1"/>
                <button type="button" class="plus">+</button>
            </div>
            <div class="goods-price">
                單價:¥<span class="shop-total-amount GoodsPrice">20.00</span>
            </div>
        </div>
        <!-- 一個商品 -->
        <div class="one-goods">
            <div class="goods-msg">
                <input type="checkbox" class="goods-check GoodsCheck">
                <button type="button" class="minus">-</button>
                <input type="text" class="am-num-text" value="1"/>
                <button type="button" class="plus">+</button>
            </div>
            <div class="goods-price">
                單價:¥<span class="shop-total-amount GoodsPrice">9.90</span>
            </div>
        </div>
        <!-- 一個商品 -->
        <div class="one-goods">
            <div class="goods-msg ">
                <input type="checkbox" class="goods-check GoodsCheck">
                <button type="button" class="minus">-</button>
                <input type="text" class="am-num-text" value="1"/>
                <button type="button" class="plus">+</button>
            </div>
            <div class="goods-price">
                單價:¥<span class="shop-total-amount GoodsPrice">10.00</span>
            </div>
        </div>
        <!-- 店鋪合計 -->
        <div class="shop-total">
            <input type="checkbox" class="goods-check ShopCheck">店鋪全選&nbsp;&nbsp;&nbsp;&nbsp;
            本店合計:¥<span class="shop-total-amount ShopTotal">0</span>
        </div>
    </div>
    <!-- 一個店鋪 -->
    <div class="one-shop">
        <!-- 一個商品 -->
        <div class="one-goods">
            <div class="goods-msg">
                <input type="checkbox" class="goods-check GoodsCheck">
                <button type="button" class="minus">-</button>
                <input type="text" class="am-num-text" value="1"/>
                <button type="button" class="plus">+</button>
            </div>
            <div class="goods-price">
                單價:¥<span class="shop-total-amount GoodsPrice">20.00</span>
            </div>
        </div>
        <!-- 一個商品 -->
        <div class="one-goods">
            <div class="goods-msg">
                <input type="checkbox" class="goods-check GoodsCheck">
                <button type="button" class="minus">-</button>
                <input type="text" class="am-num-text" value="1"/>
                <button type="button" class="plus">+</button>
            </div>
            <div class="goods-price">
                單價:¥<span class="shop-total-amount GoodsPrice">9.90</span>
            </div>
        </div>
        <!-- 一個商品 -->
        <div class="one-goods">
            <div class="goods-msg ">
                <input type="checkbox" class="goods-check GoodsCheck">
                <button type="button" class="minus">-</button>
                <input type="text" class="am-num-text" value="1"/>
                <button type="button" class="plus">+</button>
            </div>
            <div class="goods-price">
                單價:¥<span class="shop-total-amount GoodsPrice">10.00</span>
            </div>
        </div>
        <!-- 店鋪合計 -->
        <div class="shop-total">
            <input type="checkbox" class="goods-check ShopCheck">店鋪全選&nbsp;&nbsp;&nbsp;&nbsp;
            本店合計:¥<span class="shop-total-amount ShopTotal">0</span>
        </div>
    </div>
    <!-- 總計 -->
    <div class="all-total">
        <input type="checkbox" class="goods-check" id="AllCheck">全選&nbsp;&nbsp;&nbsp;&nbsp;
        總價合計:¥<span class="shop-total-amount" id="AllTotal">0</span>
    </div>
    <!--刪除-->
    <div class="all-del">
        <a id="AllDel">刪除</a>
    </div>
<script src="http://code.jquery.com/jquery-2.2.0.min.js"></script> 
<script>
  // 數量減
  $(".minus").click(function() {
    var t = $(this).parent().find('.am-num-text');
    t.val(parseInt(t.val()) - 1);
    if (t.val() <= 1) {
      t.val(1);
    }
    TotalPrice();
  });
  // 數量加
  $(".plus").click(function() {
    var t = $(this).parent().find('.am-num-text');
    t.val(parseInt(t.val()) + 1);
    if (t.val() <= 1) {
      t.val(1);
    }
    TotalPrice();
  });
  // 點擊商品按鈕
  $(".GoodsCheck").click(function() {
    var goods = $(this).closest(".one-shop").find(".GoodsCheck"); //獲取本店鋪的所有商品
    var goodsC = $(this).closest(".one-shop").find(".GoodsCheck:checked"); //獲取本店鋪所有被選中的商品
    var Shops = $(this).closest(".one-shop").find(".ShopCheck"); //獲取本店鋪的全選按鈕
    if (goods.length == goodsC.length) { //如果選中的商品等於所有商品
      Shops.prop('checked', true); //店鋪全選按鈕被選中
      if ($(".ShopCheck").length == $(".ShopCheck:checked").length) { //如果店鋪被選中的數量等於所有店鋪的數量
        $("#AllCheck").prop('checked', true); //全選按鈕被選中
        TotalPrice();
      } else {
        $("#AllCheck").prop('checked', false); //else全選按鈕不被選中 
        TotalPrice();
      }
    } else { //如果選中的商品不等於所有商品
      Shops.prop('checked', false); //店鋪全選按鈕不被選中
      $("#AllCheck").prop('checked', false); //全選按鈕也不被選中
      // 計算
      TotalPrice();
      // 計算
    }
  });
  // 點擊店鋪按鈕
  $(".ShopCheck").change(function() {
    if ($(this).prop("checked") == true) { //如果店鋪按鈕被選中
      $(this).parents(".one-shop").find(".goods-check").prop('checked', true); //店鋪內的所有商品按鈕也被選中
      if ($(".ShopCheck").length == $(".ShopCheck:checked").length) { //如果店鋪被選中的數量等於所有店鋪的數量
        $("#AllCheck").prop('checked', true); //全選按鈕被選中
        TotalPrice();
      } else {
        $("#AllCheck").prop('checked', false); //else全選按鈕不被選中
        TotalPrice();
      }
    } else { //如果店鋪按鈕不被選中
      $(this).parents(".one-shop").find(".goods-check").prop('checked', false); //店鋪內的所有商品也不被全選
      $("#AllCheck").prop('checked', false); //全選按鈕也不被選中
      TotalPrice();
    }
  });
  // 點擊全選按鈕
  $("#AllCheck").click(function() {
    if ($(this).prop("checked") == true) { //如果全選按鈕被選中
      $(".goods-check").prop('checked', true); //所有按鈕都被選中
      TotalPrice();
    } else {
      $(".goods-check").prop('checked', false); //else所有按鈕不全選
      TotalPrice();
    }
    $(".ShopCheck").change(); //執行店鋪全選的操作
  });
  //點擊刪除
  $('#AllDel').click(function(){
     $(".GoodsCheck:checked").each(function() { // 遍歷選中的商品checkbox
        var goods = $(this).closest(".one-shop").find(".GoodsCheck"); //獲取本店鋪的所有商品
        var goodsC = $(this).closest(".one-shop").find(".GoodsCheck:checked"); //獲取本店鋪所有被選中的商品
        var Shops = $(this).closest(".one-shop").find(".ShopCheck"); //獲取本店鋪的全選按鈕
         if (goods.length == goodsC.length) { //如果選中的商品等於所有商品
            Shops.prop('checked', true); //店鋪全選按鈕不被選中
            $(this).closest(".one-shop").remove();
            TotalPrice();
         } else { //如果選中的商品不等於所有商品
         var n = $(this).parents('.one-goods').index(); // 獲取checkbox所在行的順序
            $(".one-shop").find(".one-goods:eq("+n+")").remove();
            TotalPrice();
         }
     });
  });

  function TotalPrice() {
    var allprice = 0; //總價
    $(".one-shop").each(function() { //循環每個店鋪
      var oprice = 0; //店鋪總價
      $(this).find(".GoodsCheck").each(function() { //循環店鋪里面的商品
        if ($(this).is(":checked")) { //如果該商品被選中
          var num = parseInt($(this).parents(".one-goods").find(".am-num-text").val()); //得到商品的數量
          var price = parseFloat($(this).parents(".one-goods").find(".GoodsPrice").text()); //得到商品的單價
          var total = price * num; //計算單個商品的總價
          oprice += total; //計算該店鋪的總價
        }
        $(this).closest(".one-shop").find(".ShopTotal").text(oprice.toFixed(2)); //顯示被選中商品的店鋪總價
      });
      var oneprice = parseFloat($(this).find(".ShopTotal").text()); //得到每個店鋪的總價
      allprice += oneprice; //計算所有店鋪的總價
    });
    $("#AllTotal").text(allprice.toFixed(2)); //輸出全部總價
  }
  </script>
</body>
</html>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM