jQuery購物車

來源:互聯網
上載者:User

標籤:技術   als   htm   int   else   image   收藏   button   nbsp   

  1. HTML代碼:(非表格方式)

    <div class="nav2">
    <input type="checkbox" class="checkAll" />&emsp;全選
    <span>商品資訊</span>
    <ul class="fr">
    <li>單價</li>
    <li>數量</li>
    <li>小計</li>
    <li>操作</li>
    </ul>
    </div>



    <div class="goods">
    <input type="checkbox" class="check"/>
    <span class="img"><img src="../img/d.png" /></span>
    <p class="goods_p">
    耐克(nike)802611-001 AIR<br />
    MAX減震複古休閑鞋 運動鞋 奧<br />
    利奧配色 US9碼42.5碼
    </p>
    <p class="goods_p1">
    顏色:黑色<br />
    尺碼:40.5
    </p>
    <ul class="fr">
    <li>
    <span class="goods_sp">¥999.00</span><br />
    <span class="goods_sp1">¥<span class="price">748.00</span></span><br />
    <span class="goods_sp2">賣家促銷</span>
    </li>

    <li><span class="goods_sp3"><img src="../img/jia.png" class="cut" />
    <span class="num">1</span>
    <img src="../img/jia1.png" class="add"/></span>
    </li>
    <li><span class="goods_sp4">¥<span class="total">748</span></span></li>
    <li>
    <button >移入我的收藏</button>.
    <button >刪除</button>
    </li>
    </ul>

    </div>

    <div class="goods">
    <input type="checkbox" class="check"/>
    <span class="img"><img src="../img/d.png" /></span>
    <p class="goods_p">
    耐克(nike)802611-001 AIR<br />
    MAX減震複古休閑鞋 運動鞋 奧<br />
    利奧配色 US9碼42.5碼
    </p>
    <p class="goods_p1">
    顏色:黑色<br />
    尺碼:40.5
    </p>
    <ul class="fr">
    <li >
    <span class="goods_sp">¥999.00</span><br />
    <span class="goods_sp1">¥<span class="price">748.00</span></span><br />
    <span class="goods_sp2">賣家促銷</span>
    </li>

    <li><span class="goods_sp3"><img src="../img/jia.png" class="cut" />
    <span class="num">1</span>
    <img src="../img/jia1.png" class="add"/></span>
    </li>
    <li><span class="goods_sp4">¥<span class="total">748</span></span></li>
    <li>
    <button >移入我的收藏</button>.
    <button >刪除</button>
    </li>
    </ul>

    </div>

    <div class="goods2">
    <input type="checkbox" class="checkAll1" />
    <span class="goods2_sp">全選</span>
    <span class="goods2_sp1"><a href="#">刪除選擇中的商品</a>
    &emsp;<a href="#">移到我的關注清單</a>
    &emsp;<a href="#">清除下櫃商品</a>
    </span>

    <p class="goods2_p">已選擇<span class="color" id="allNum">0</span>件商品
    <img src="../img/sanjiao.png" />
    </p>
    <p class="goods2_p1">
    總價:<span class="color" id="all">¥0.00</span>
    <br />
    已節省:-¥0.00
    </p>

    <button>去結算</button>
    </div>

  2. jQuery代碼:

    $(function() {

    $(".add").click(function(e) {
    var quantity = parseInt($(this).prev().text());
    $(this).prev().text(quantity + 1);
    //計算每件商品總價
    var price = parseInt($(this).parents("li").prev().find(".price").text());
    var num = parseInt($(this).prev().text());
    $(this).parents(‘li‘).next().find(‘.total‘).text((price * num).toFixed(2));
    e.preventDefault()
    setTotal();
    });
    //減的計算
    $(".cut").click(function(e) {
    var quantity = parseInt($(this).next().text());

    if(quantity > 1) {
    $(this).next().text(quantity - 1);
    };

    var price = parseInt($(this).parents("li").prev().find(".price").text());
    var num = parseInt($(this).next().text());
    //小計
    $(this).parents(‘li‘).next().find(‘.total‘).text((price * num).toFixed(2));
    e.preventDefault()
    setTotal();
    });
    //計算總價
    function setTotal() {

    var total = 0;
    var totalPrice = 0;
    //$(‘.check‘).each(function() {
    //if($(this).hasClass("status")) {
    $(".status .num").each(function() {
    total += parseInt($(this).text());
    });
    $(".status .total").each(function() {
    totalPrice += parseInt($(this).text());
    });

    $("#allNum").text(total);
    $("#all").text(totalPrice);
    //}
    //});

    }

    //全選
    $(‘.checkAll‘).click(function(){
    if($(‘.checkAll‘).prop("checked")==true){
    $(‘.check‘).prop("checked",true);
    $(‘.check‘).parent().addClass("status");
    setTotal();
    }else{
    $(‘.check‘).prop("checked",false);
    $(‘.check‘).parent().removeClass("status");
    setTotal();
    }
    });
    // 單選
    $(".check").click( function(){
    $(this).each(function(){
    if($(this).prop("checked")==true){
    $(this).parent().addClass("status");//添加勾選樣式
    setTotal();
    }else{
    $(this).parent().removeClass("status");//取消勾選樣式
    setTotal();
    }
    });
    });});

 

jQuery購物車

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.