jquery实现购物车数量加减,价格计算功能

发布时间:2020-06-10 10:22:24 作者:frwupeng517
来源:网络 阅读:10471

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实现购物车数量加减,价格计算功能

推荐阅读:
  1. jQuery实现飞入购物车功能
  2. jQuery实现购物车物品数量的加减

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

js 购物车 完整功能

上一篇:以后Jquery会被HTML5取代嘛,

下一篇:linux查看进程是否存在的方法

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》