購物車功能:使用jQuery實(shí)現(xiàn)購物車全選反選玩祟,單選腹缩,商品增刪,小計(jì)等功能

效果圖:

canvas.png

html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>模擬購物車功能-jq</title>
    <link rel="stylesheet" href="bootstrap-3.3.7-dist/css/bootstrap.min.css" />
     <link rel="stylesheet" href="css/shopCart.css" />
    <script type="text/javascript" src="js/jquery-2.1.0.js" ></script>
</head>
<body>
    
    <table class="table">
        <tr>
            <th id="checkAll"><label><input type="checkbox" checked  />全選</label><button>刪除</button></th>
            <th>商品名稱</th>
            <th>商品價(jià)格(元)</th>
            <th>數(shù)目</th>
            <th>小計(jì)(元)</th>
            <th>操作</th>
        </tr>
        <tr>
            <td class="check"><label><input type="checkbox"  checked /></label></td>
            <td>商品名稱1</td>
            <td class="price">22.50</td>
            <td><span class="sub">-</span><input type="text" value="1" class="num"/><span class="add">+</span></td>
            <td class="subtotal">22.50</td>
            <td class="del"><button>刪除</button></td>
        </tr>
        <tr>
            <td class="check"><label><input type="checkbox" checked /></label></td>
            <td>商品名稱2</td>
            <td class="price">12.50</td>
            <td><span class="sub">-</span><input type="text" value="1" class="num"/><span class="add">+</span></td>
            <td class="subtotal">12.50</td>
            <td class="del"><button>刪除</button></td>
        </tr>
        <tr>
            <td class="check"><label><input type="checkbox" checked /></label></td>
            <td>商品名稱3</td>
            <td class="price">110.40</td>
            <td><span class="sub">-</span><input type="text" value="1" class="num"/><span class="add">+</span></td>
            <td class="subtotal">110.40</td>
            <td class="del"><button>刪除</button></td>
        </tr>
        <tr>
            <td colspan="5" style="text-align: right;">總件數(shù):<i id="numAll">0</i>件  &nbsp; &nbsp;   總計(jì):<i id="total">0.00</i>元</td>
        </tr>
    </table>
    
    <script src="js/jquery-2.1.0.js" type="text/javascript" charset="utf-8"></script>
    <script src="js/shopCart.js" type="text/javascript" charset="utf-8"></script>
    
</body>
</html>

css樣式:

*{
    margin:0;
    padding: 0;
}

table th,table td,input{
    text-align: center;
}
table #checkAll{
    width: 150px;
}
table #checkAll label{
    cursor: pointer;
    background: url(../img/confirm.png) no-repeat center left;
    padding-left:10px;
}
 table .check label{
    cursor: pointer;
    background: url(../img/confirm.png) no-repeat center;
}

table #checkAll input, table .check input{
    visibility: hidden;
    
}

table input[type="text"]{
    width: 50px;
    overflow: hidden;
}
table span{
    display: inline-block;
    width: 20px;
    background: #8C8C8C;
    margin:0px 5px ;
    color: #FFFFFF;
    cursor: pointer;
}

js:

$(function() {

    // 全選
    $("#checkAll input").click(function() {
        var flag = $(this).prop("checked");
        if(flag) {
            $(".check label input").prop("checked", true);

            $("#checkAll label").css("background", "url(img/confirm.png) no-repeat center left");
            $(".check label").css("background", "url(img/confirm.png) no-repeat center");

        } else {
            $(".check label input").prop("checked", false);

            $("#checkAll label").css("background", "url(img/confirm_no.png) no-repeat center left");
            $(".check label").css("background", "url(img/confirm_no.png) no-repeat center");
        }
        counts();
        totalPrice();
    });

    //單選
    $(".check input").click(function() {
        var flag = $(this).prop("checked"); //獲取當(dāng)前input的狀態(tài)
        var CL = $(".check input").length; //列表長度;
        var CH = $(".check input:checked").length; //列表中被選中的長度

        if(flag) {
            $(this).parent("label").css("background", "url(img/confirm.png) no-repeat center");
        } else {
            $(this).parent("label").css("background", "url(img/confirm_no.png) no-repeat center");
        }

        if(CL == CH) {
            $("#checkAll input").prop("checked", true);
            $("#checkAll label").css("background", "url(img/confirm.png) no-repeat center left");
        } else {
            $("#checkAll input").prop("checked", false);
            $("#checkAll label").css("background", "url(img/confirm_no.png) no-repeat center left");
        }
        counts();
        totalPrice();
    })

    //數(shù)目加
    $(".add").click(function() {
        var num = $(this).prev().val();
        var price = parseFloat($(this).parent().siblings(".price").text());
        num++;
        $(this).prev().val(num);

        //      小計(jì)
        $(this).parent().siblings(".subtotal").text((price * num).toFixed(2));
        counts();
        totalPrice();
    })

    //數(shù)目減
    $(".sub").click(function() {
        var num = $(this).next().val();
        var price = parseFloat($(this).parent().siblings(".price").text());
        num--;

        if(num <= 0) {
            num = 0
        }
        $(this).next().val(num);

        //      小計(jì)
        $(this).parent().siblings(".subtotal").text((price * num).toFixed(2));
        counts();
        totalPrice();
    })

    //文本框脫里焦點(diǎn)處理
    $('.num').each(function(i) {
        $(this).blur(function() {
            let p = parseFloat($(this).parents('tr').find(".subtotal").text());
            let c = parseFloat(this.value);
            console.log(p*c);
            $(this).parents('tr').find(".subtotal").text((c * p).toFixed(2));
            counts();
            totalPrice();
        })
    })

    //單行刪除
    $(".del button").click(function() {
        var flag = $(this).parent().siblings().find("input").prop("checked");
        if(flag) {
            if(confirm("是否確定刪除")) {
                $(this).parents("tr").remove();
                var CL = $(".check input").length; //列表長度藏鹊;
                if(CL == 0) {
                    $("#checkAll label input").prop("checked", false);
                    $("#checkAll label").css("background", "url(img/confirm_no.png) no-repeat center left");
                }
                counts();
                totalPrice();
            }
        }
    })

    //全刪除
    $("#checkAll button").click(function() {
        var flag = $(this).prev().children().prop("checked");
        //        console.log(flag);
        if(flag) {

            if(confirm("是否確定刪除")) {

                $(".check").parent().remove();
                var CL = $(".check input").length; //列表長度润讥;

                if(CL == 0) {
                    $("#checkAll label input").prop("checked", false);
                    $("#checkAll label").css("background", "url(img/confirm_no.png) no-repeat center left");
                }
                counts();
                totalPrice();
            }

        }
    })

    //總價(jià)格
    totalPrice();

    function totalPrice() {
        var prices = 0;
        $('.check input:checked').each(function(i) {
            console.log()
            prices += parseFloat($(this).parents("tr").find('.subtotal').text());
        })
        $('#total').text(prices);
    }
    //總數(shù)目
    counts();

    function counts() {
        var sum = 0;
        $('.check input:checked').each(function(i) {
            sum += parseInt($(this).parents("tr").find('.num').val());
        })
        $('#numAll').text(sum);
    }

})
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市盘寡,隨后出現(xiàn)的幾起案子楚殿,更是在濱河造成了極大的恐慌,老刑警劉巖竿痰,帶你破解...
    沈念sama閱讀 218,036評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件脆粥,死亡現(xiàn)場離奇詭異,居然都是意外死亡影涉,警方通過查閱死者的電腦和手機(jī)变隔,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,046評論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來常潮,“玉大人弟胀,你說我怎么就攤上這事『笆剑” “怎么了孵户?”我有些...
    開封第一講書人閱讀 164,411評論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長岔留。 經(jīng)常有香客問我夏哭,道長,這世上最難降的妖魔是什么献联? 我笑而不...
    開封第一講書人閱讀 58,622評論 1 293
  • 正文 為了忘掉前任竖配,我火速辦了婚禮,結(jié)果婚禮上里逆,老公的妹妹穿的比我還像新娘进胯。我一直安慰自己,他們只是感情好原押,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,661評論 6 392
  • 文/花漫 我一把揭開白布胁镐。 她就那樣靜靜地躺著,像睡著了一般诸衔。 火紅的嫁衣襯著肌膚如雪盯漂。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,521評論 1 304
  • 那天笨农,我揣著相機(jī)與錄音就缆,去河邊找鬼。 笑死谒亦,一個胖子當(dāng)著我的面吹牛竭宰,可吹牛的內(nèi)容都是我干的空郊。 我是一名探鬼主播,決...
    沈念sama閱讀 40,288評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼羞延,長吁一口氣:“原來是場噩夢啊……” “哼渣淳!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起伴箩,我...
    開封第一講書人閱讀 39,200評論 0 276
  • 序言:老撾萬榮一對情侶失蹤入愧,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后嗤谚,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體棺蛛,經(jīng)...
    沈念sama閱讀 45,644評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,837評論 3 336
  • 正文 我和宋清朗相戀三年巩步,在試婚紗的時候發(fā)現(xiàn)自己被綠了旁赊。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,953評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡椅野,死狀恐怖终畅,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情竟闪,我是刑警寧澤离福,帶...
    沈念sama閱讀 35,673評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站炼蛤,受9級特大地震影響妖爷,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜理朋,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,281評論 3 329
  • 文/蒙蒙 一絮识、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧嗽上,春花似錦俯在、人聲如沸她紫。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,889評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽纽谒。三九已至风范,卻和暖如春百宇,著一層夾襖步出監(jiān)牢的瞬間疹尾,已是汗流浹背洛史。 一陣腳步聲響...
    開封第一講書人閱讀 33,011評論 1 269
  • 我被黑心中介騙來泰國打工惯殊, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人也殖。 一個月前我還...
    沈念sama閱讀 48,119評論 3 370
  • 正文 我出身青樓土思,卻偏偏與公主長得像务热,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子己儒,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,901評論 2 355

推薦閱讀更多精彩內(nèi)容