JQuery 實現 購物車頁面 實現簡單功能,(包括計算,刪除)


效果圖:

 

 

 

 

源代碼:

 

樣式設置:

<!--樣式的設置-->
    <style type="text/css">
        body {
            margin: 0px;
            padding: 0px;
            font-size: 12px;
            line-height: 20px;
            color: #333;
        }
        
        ul,li {
            list-style: none;
            margin: 0px;
            padding: 0px;
        }
        
        a {
            color: #1965h3;
            text-decoration: none;
        }
        
        a:hover {
            color: #cd590c;
            text-decoration: underline;
        }
        
        img {
            border: 0px;
            vertical-align: middle;
        }
        
        #header {
            height: 40px;
            margin: 10px auto 10px auto;
            width: 800px;
            clear: both;
        }
        
        #nav {
            margin: 10px auto 10px auto;
            width: 800px;
            clear: both;
        }
        
        #navlist {
            width: 800px;
            margin: 0px auto 0px auto;
            height: 23px;
        }
        
        #navlist li {
            float: left;
            height: 23px;
            line-height: 26px;
        }
        
        .navlist_red_left {
            background-image: url(img/register_bg.gif);
            background-repeat: no-repeat;
            background-position: -12px -92px;
            width: 3px;
        }
        
        .navlist_red {
            background-color: #ff6600;
            text-align: center;
            font-size: 14px;
            font-weight: bold;
            color: #FFF;
            width: 130px;
        }
        
        .navlist_red_arrow {
            background-color: #ff6600;
            background-image: url(img/register_bg.gif);
            background-repeat: no-repeat;
            background-position: 0px 0px;
            width: 13px;
        }
        
        .navlist_gray {
            background-color: #e4e4e4;
            text-align: center;
            font-size: 14px;
            font-weight: bold;
            width: 150px;
        }
        
        .navlist_gray_arrow {
            background-color: #e4e4e4;
            background-image: url(img/register_bg.gif);
            background-repeat: no-repeat;
            background-position: 0px 0px;
            width: 13px;
        }
        
        .navlist_gray_right {
            background-image: url(img/register_bg.gif);
            background-repeat: no-repeat;
            background-position: -12px -138px;
            width: 3px;
        }
        
        #content {
            width: 800px;
            margin: 10px auto 5px auto;
            clear: both;
        }
        
        .title_1 {
            text-align: center;
            width: 50px;
        }
        
        .title_2 {
            text-align: center;
        }
        
        .title_3 {
            text-align: center;
            width: 80px;
        }
        
        .title_4 {
            text-align: center;
            width: 80px;
        }
        
        .title_5 {
            text-align: center;
            width: 100px;
        }
        
        .title_6 {
            text-align: center;
            width: 80px;
        }
        
        .title_7 {
            text-align: center;
            width: 60px;
        }
        
        .line {
            background-color: #a7cbff;
            height: 3px;
        }
        
        .shopInfo {
            padding-left: 10px;
            height: 35px;
            vertical-align: bottom;
        }
        
        .num_input {
            border: solid 1px #666;
            width: 25px;
            height: 15px;
            text-align: center;
        }
        
        .td_1,
        .td_2,
        .td_3,
        .td_4,
        .td_5,
        .td_6,
        .td_7,
        .td_8 {
            background-color: #e2f2ff;
            border-bottom: solid 1px #d1ecff;
            border-top: solid 1px #d1ecff;
            text-align: center;
            padding: 5px;
        }
        
        .td_1,
        .td_3,
        .td_4,
        .td_5,
        .td_6,
        .td_7 {
            border-right: solid 1px #FFF;
        }
        
        .td_3 {
            text-align: left;
        }
        
        .td_4 {
            font-weight: bold;
        }
        
        .td_7 {
            font-weight: bold;
            color: #fe6400;
            font-size: 14px;
        }
        
        .hand {
            cursor: pointer;
        }
        
        .shopend {
            text-align: right;
            padding-right: 10px;
            padding-bottom: 10px;
        }
        
        .yellow {
            font-weight: bold;
            color: #FE6400;
            font-size: 18px;
        }
    </style>

jQuery 實現功能

         <!--  用Jquery 實現相應功能-->
    <script type="text/javascript" src="js/jquery-1.8.3.js"></script>
    <script type="text/javascript">
        $(function() {
            //點擊復選框全選或不全選效果
            $("#allCheckBok").click(function() {
                var checked = $(this).is(":checked");
                $(".td_1").children().attr("checked", checked);
            });

            //判斷是否全選
            function Allchk() {
                var checkedB = $(".td_1").children();
                var sum = checkedB.size();
                var k = 0;
                checkedB.each(function(index, dom) {
                    if($(dom).is(":checked"))
                        k++;
                });

                if(k == sum) {
                    $("#allCheckBok").attr("checked", true);
                } else {
                    $("#allCheckBok").attr("checked", false)
                }

            }

            Allchk(); //頁面加載完后運行

            //單選判斷
            $(".td_1").children().click(function() {
                Allchk();
            })

            //計算總價與小計
            function prodC() {
                var $tr = $("#shopping").find("tr[id]");
                var summer = 0;
                var integral = 0;
                $tr.each(function(i, dom) {
                    var num = $(dom).children(".td_6").find("input").val(); //商品數量
                    var price = num * $(dom).children(".td_5").text(); //商品小計
                    $(dom).children(".td_7").html(price); //顯示商品小計
                    summer += price; //總價
                    integral += $(dom).children(".td_4").text() * num; //積分
                });
                $("#total").text(summer);
                $("#integral").text(integral);
            }
            prodC(); //頁面加載完成后運行

            //商品增加減少 ,flag 為true時增加 flag為false時減少
            function changN(dom, flag) {
                var $input = $(dom).parent().find("input");
                var value = $input.val();
                if(flag) {
                    value++;
                } else {
                    value--;
                    if(value < 0) {
                        value = 0;
                        alert("寶貝數量不能為負值")
                    }
                }
                $input.val(value);
                prodC();
            };

            //點擊增加
            $(".td_6").find("img[alt='minus']").click(function() {
                changN(this, false);
            });
            //點擊減少
            $(".td_6").find("img[alt='add']").click(function() {
                changN(this, true);
            });

            //點擊刪除
            $(".td_8").find("a").click(function() {
                $(this).parent().parent().prev().remove(); //刪除前一tr
                $(this).parent().parent().remove(); //刪除當前tr
                prodC();
            });
            //點擊刪除所選
            $("#deleteAll").click(function() {
                $("#shopping").find("tr[id]").each(function(i, e) {
                    var $tr = $(e);
                    var checked = $tr.children(".td_1").children().is(":checked");
                    if(checked) {
                        $tr.prev().remove();
                        $tr.remove();
                    }
                });
                prodC()

            });

        })
    </script>


布局

<body>
        <div id="header"><img src="img/taobao_logo.gif" alt="logo"></div>
        <div id="nav">您的位置:
            <a href="#">首頁</a>&gt;
            <a href="#">我的淘寶</a>&gt;我的購物車</div>
        <div id="navlist">
            <ul>
                <li class="navlist_red_left"></li>
                <li class="navlist_red">1.查看購物車</li>
                <li class="navlist_red_arrow"></li>
                <li class="navlist_gray">2.確定訂單信息</li>
                <li class="navlist_gray_arrow"></li>
                <li class="navlist_gray">3.付款到支付寶</li>
                <li class="navlist_gray_arrow"></li>
                <li class="navlist_gray">4.確定收貨</li>
                <li class="navlist_gray_arrow"></li>
                <li class="navlist_gray">5.評價</li>
                <li class="navlist_gray_right"></li>
            </ul>
        </div>

        <div id="content">
            <form action="" method="post" name="myform">
                <table width="100%" border="0" cellpadding="0" cellspacing="0" id="shopping">
                    <tr>
                        <td class="title_1"><input id="allCheckBok" type="checkbox" value="">全選</td>
                        <td class="title_2" colspan="2">店鋪寶貝</td>
                        <td class="title_3">獲積分</td>
                        <td class="title_4">單價(元)</td>
                        <td class="title_5">數量</td>
                        <td class="title_6">小計(元)</td>
                        <td class="title_7">操作</td>
                    </tr>

                    <tr>
                        <td colspan="8" class="line"></td>
                    </tr>

                    <tr>
                        <td colspan="8" class="shopInfo">店鋪:
                            <a href="#">纖巧百媚時尚鞋坊</a> 賣家:
                            <a href="#">纖巧百媚</a><img src="img/taobao_relation.jpg"></td>
                    </tr>
                    <tr id="product1">
                        <td class="td_1"><input type="checkbox" name="cartCheckBox" value="product1"></td>
                        <td class="td_2"><img src="img/taobao_cart_01.jpg" alt="shopping" </td>
                            <td class="td_3">
                                <a href="#">日韓流行風時尚美眉最愛獨特米字拼圖金屬坡跟公主靴子黑色</a><br /> 顏色:棕色 尺碼:37<br /> 保障:
                                <img src="img/taobao_icon_01.jpg" /> </td>
                            <td class="td_4">5</td>
                            <td class="td_5">138.00</td>
                            <td class="td_6"><img src="img/taobao_minus.jpg" alt="minus" class="hand"> <input type="text" id="num_1" value="1" class="num_input" readonly="readonly"> <img src="img/taobao_adding.jpg" alt="add" class="hand" /></td>
                            <td class="td_7"></td>
                            <td class="td_8">
                                <a href="javascript:void(0):">刪除</a>
                            </td>
                    </tr>

                    <tr>
                        <td colspan="8" class="shopInfo">店鋪:
                            <a href="#">香港我的美麗日記</a> 賣家:
                            <a href="#">lokemick2009</a> <img src="img/taobao_relation.jpg" alt="relation" /></td>
                    </tr>
                    <tr id="product2">
                        <td class="td_1"><input name="cartCheckBox" type="checkbox" value="product2" /></td>
                        <td class="td_2"><img src="img/taobao_cart_02.jpg" alt="shopping" /></td>
                        <td class="td_3">
                            <a href="#">chanel/香奈爾/香奈爾炫亮魅力唇膏3.5g</a><br /> 保障:
                            <img src="img/taobao_icon_01.jpg" alt="icon" /> <img src="img/taobao_icon_02.jpg" alt="icon" /></td>
                        <td class="td_4">12</td>
                        <td class="td_5">265.00</td>
                        <td class="td_6"><img src="img/taobao_minus.jpg" alt="minus" class="hand" /> <input id="num_2" type="text" value="1" class="num_input" readonly="readonly" /> <img src="img/taobao_adding.jpg" alt="add" class="hand" /></td>
                        <td class="td_7"></td>
                        <td class="td_8">
                            <a href="javascript:void(0);">刪除</a>
                        </td>
                    </tr>

                    <tr>
                        <td colspan="8" class="shopInfo">店鋪:
                            <a href="#">實體經營</a> 賣家:
                            <a href="#">林顏店鋪</a> <img src="img/taobao_relation.jpg" alt="relation" /></td>
                    </tr>
                    <tr id="product3">
                        <td class="td_1"><input name="cartCheckBox" type="checkbox" value="product3" /></td>
                        <td class="td_2"><img src="img/taobao_cart_03.jpg" alt="shopping" /></td>
                        <td class="td_3">
                            <a href="#">蝶妝海?藍清瀅粉底液10#(象牙白)</a><br /> 保障:
                            <img src="img/taobao_icon_01.jpg" alt="icon" /> <img src="img/taobao_icon_02.jpg" alt="icon" /></td>
                        <td class="td_4">3</td>
                        <td class="td_5">85.00</td>
                        <td class="td_6"><img src="img/taobao_minus.jpg" alt="minus" class="hand" /> <input id="num_3" type="text" value="1" class="num_input" readonly="readonly" /> <img src="img/taobao_adding.jpg" alt="add" class="hand" /></td>
                        <td class="td_7"></td>
                        <td class="td_8">
                            <a href="javascript:void(0);">刪除</a>
                        </td>
                    </tr>

                    <tr>
                        <td colspan="8" class="shopInfo">店鋪:
                            <a href="#">紅豆豆的小屋</a> 賣家:
                            <a href="#">taobao豆豆</a> <img src="img/taobao_relation.jpg" alt="relation" /></td>
                    </tr>
                    <tr id="product4">
                        <td class="td_1"><input name="cartCheckBox" type="checkbox" value="product4" /></td>
                        <td class="td_2"><img src="img/taobao_cart_04.jpg" alt="shopping" /></td>
                        <td class="td_3">
                            <a href="#">相宜促銷專供 大S推薦 最好用的LilyBell化妝棉</a><br /> 保障:
                            <img src="img/taobao_icon_01.jpg" alt="icon" /></td>
                        <td class="td_4">12</td>
                        <td class="td_5">12.00</td>
                        <td class="td_6"><img src="img/taobao_minus.jpg" alt="minus" class="hand" /> <input id="num_4" type="text" value="2" class="num_input" readonly="readonly" /> <img src="img/taobao_adding.jpg" alt="add" class="hand" /></td>
                        <td class="td_7"></td>
                        <td class="td_8">
                            <a href="javascript:void(0);">刪除</a>
                        </td>
                    </tr>

                    <tr>
                        <td colspan="3">
                            <a id="deleteAll" href="javascript:void(0)"><img src="img/taobao_del.jpg" alt="delete" /></a>
                        </td>
                        <td colspan="5" class="shopend">商品總價(不含運費):<label id="total" class="yellow"></label><br /> 可獲積分 <label class="yellow" id="integral"></label><br />
                            <input name="" type="image" src="img/taobao_subtn.jpg" /></td>
                    </tr>

                </table>

            </form>
        </div>

    </body>

 


免責聲明!

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



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