91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

jquery實現購物車數量加減,價格計算功能

發布時間:2020-06-10 10:22:24 來源:網絡 閱讀:10471 作者:frwupeng517 欄目:web開發

HTML代碼:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="renderer" content="webkit">
    <title>A03號桌</title>
    <link rel="stylesheet" href="resources/css/main.css">
</head>
<body>
    <!--購物車-->
    <div class="shopCart"> 
    <!--可以在table外面套一個div寫死寬高并設置overflow-y:scroll;,出現大量內容時,讓table縱向滾動-->
       <div class="cartBox"> 
            <table class="cart">
                <thead>
                <tr>
                    <th>菜品名稱</th>
                    <th>數量</th>
                    <th>單價</th>
                    <th>價格</th>
                </tr>
                </thead>
                <tbody>
                <tr>
                    <td>大閘蟹</td>
                    <td>
                        <button class="add">+</button>
                        <span class="count">1</span>
                        <button class="reduce">-</button>
                    </td>
                    <td>
                        ¥<span class="price">68.00</span>
                    </td>
                    <td>
                        ¥<span class="sub_total">68.00</span>
                    </td>
                </tr>
                <tr>
                    <td>在天愿作比翼鳥</td>
                    <td>
                        <button class="add">+</button>
                        <span class="count">1</span>
                        <button class="reduce">-</button>
                    </td>
                    <td>
                        ¥<span class="price">68.00</span>
                    </td>
                    <td>
                        ¥<span class="sub_total">68.00</span>
                    </td>
                </tr>
                <tr>
                    <td>紅嘴綠鸚哥</td>
                    <td>
                        <button class="add">+</button>
                        <span class="count">1</span>
                        <button class="reduce">-</button>
                    </td>
                    <td>
                        ¥<span class="price">68.00</span>
                    </td>
                    <td>
                        ¥<span class="sub_total">68.00</span>
                    </td>
                </tr>
                </tbody>
            </table>
        </div>

        <ul class="totalInfo clearfix">
            <li>
                <span class="total">
                合計:<i>¥</i><b>242.00</b>
                </span>
            </li>
            <li>
                <button class="btn-save">保存</button>
            </li>
        </ul>
    </div>


<script src="resources/js/jquery-1.8.3.min.js"></script>
<script src="resources/js/shopCart.js"></script>
</body>
</html>


JS代碼:

/****點擊增加按鈕****/
$('.add').click(function(){
    //修改數量
    var n=$(this).next().html();
    var num=parseInt(n)+1;
    $(this).next().html(num);
    //計算價格
    var c= $(this).parent().siblings().children('.price').html();
    parseInt(c);
    var subPrice = num * c;
    var sub_price = subPrice.toFixed(2); //保留小數點后面兩位小數
    $(this).parent().siblings().children('.sub_total').html(sub_price);

    //計算總價
    var total=0;
    $('.sub_total').each(function(){
        var price=parseInt($(this).html());
        total+=price;
        var total_price = total.toFixed(2);
        $('.total b').html(total_price);
    });
});


/****點擊減少按鈕****/
$('.reduce').click(function(){
    //修改數量
    var n=$(this).prev().html();
    var num=parseInt(n)-1;
    if(num==0){return;}//數量減到0就能再減了
    $(this).prev().html(num);

    //計算價格
    var c= $(this).parent().siblings().children('.price').html();
    parseInt(c);
    var subPrice = num * c;
    subPrice.toFixed(2);
    var sub_price = subPrice.toFixed(2);
    $(this).parent().siblings().children('.sub_total').html(sub_price);

    //計算總價
    var total=0;
    $('.sub_total').each(function(){
        var price=parseInt($(this).html());
        total+=price;
        var total_price = total.toFixed(2);
        $('.total b').html(total_price);
    });
});


考慮到篇幅問題,沒有貼出CSS代碼,最終頁面截圖如下:

jquery實現購物車數量加減,價格計算功能

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

刚察县| 青川县| 宁明县| 桐庐县| 滨海县| 揭东县| 谷城县| 昌都县| 怀宁县| 微博| 荔浦县| 渑池县| 赞皇县| 青河县| 临沭县| 阿合奇县| 兴文县| 仲巴县| 抚州市| 闽侯县| 临汾市| 会理县| 乳源| 乌鲁木齐县| 聊城市| 儋州市| 乐至县| 洪泽县| 沾化县| 兰西县| 安图县| 凤阳县| 喀喇| 石楼县| 隆子县| 郁南县| 河北省| 东兰县| 北辰区| 巨野县| 马尔康县|