#独家
jquery计算总计,没有小计直接计算怎么写?

2023-04-19 0 3,851
$(function(){

            getAll()
            var max_num = 100;
            var min_num = 1;

            $('.sub').click(function() {
                var n = $(this).siblings('.num').val();
                n--;
                if (n < min_num) {
                    alert("商品已达到下限")
                    n = min_num;
                } else {
                    $(this).siblings('.num').val(n);
                }
            
                getAll()

            })

            $('.add').click(function() {
                var n = $(this).siblings('.num').val();
                n++;
                if (n > max_num) {
                    alert("商品到达上限")
                    n = max_num
                } else {
                    $(this).siblings('.num').val(n);
                }
                getAll()
            })

            $('.del').click(function() {
                $(this).parents('.sect').remove();
                getAll()
            })


            function getAll() {
                var count = 0;
                var moneys = 0;
                $('.mrb').each(function(index, item) {
                    count += Number($(item).children('input').val());
                })


                $('.f1 em').text(count);

            }
        })

html:

<header>
    <div class="nav">
        <div class="nav-return">
            <a href="./index.html" rel="external nofollow" >
                <span class="iconfont icon-fanhui"></span>
            </a>
        </div>
        <div class="nav-shopcar">
            购物车
        </div>
        <div class="nav-index">
            <a href="index.html" rel="external nofollow" ><span class="iconfont icon-shouye"></span></a>
        </div>
    </div>
</header>

<div class="section">
    <div class="sect">
        <div class="sect-img">
            <img src="img/d_sp_04.png" alt="">
        </div>
        <div class="sect-text">
            <div class="secx">
                <span>长虹取暖器 家用暖风机电器省电居浴室防水对流暖气</span>
                <p class="nor">¥42.00</p>
                <div class="mrb">
                    <button class="sub">-</button>
                    <input class="num" type="text" value="1">
                    <button class="add">+</button>
                </div>
                <div class="del">
                    <div class="iconfont icon-shanchu"></div>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- 底部 -->

<div class="footer ">

    <p class="f1">共购买 <a href="#" rel="external nofollow"  class="allnum"><em>12</em></a>件,总计:<span class="iae">4</span>元</p>
    <p class="f2">去结算</p>
</div>

css:

* {
    margin: 0;
    padding: 0;
}

body {
    width: 100%;
    margin: 0 auto;
    background: #ddd4d4;
}

ul li {
    list-style: none;
}

a {
    text-decoration: none;
}


/*  */

header {
    background: red;
    width: 100%;
    height: 60px;
    margin-bottom: 10px;
    position: fixed;
    z-index: 1000;
    top: 0;
    left: 0;
}

.nav {
    font-size: 24px;
    display: flex;
    justify-content: space-between;
    width: 92%;
    height: 60px;
    line-height: 60px;
    color: #fff;
}

.nav-return {
    width: 30px;
    height: 20px;
}

.nav-shopcar {
    height: 40px;
}

.nav-index .iconfont {
    margin-left: 10px;
    color: #fff;
}

.section {
    width: 100%;
    margin-top: 62px;
    margin-bottom: 62px;
}

.sect {
    width: 98%;
    height: 176px;
    margin: 0 auto;
    background-color: #fff;
    display: flex;
    margin-bottom: 10px;
}

.sect-img {
    width: 36%;
    height: 140px;
}

.sect-img img {
    width: 80%;
    height: 140px;
    margin-top: 20px;
    margin-left: 10px;
}

.sect-text {
    width: 60%;
    height: 80%;
    margin-top: 5%;
}

.secx p {
    margin-top: 10px;
    color: red;
    margin-bottom: 10px;
}

.mrb {
    width: 90%;
    height: 40px;
    line-height: 40px;
    float: left;
}

.mrb input {
    height: 50%;
    width: 14%;
    margin-top: 4px;
    font-size: 14px;
    text-align: center;
}

.mrb button {
    width: 20%;
    height: 100%;
    border-radius: 50%;
    background-color: #aca1a1;
    border: none;
    margin-right: 5px;
    margin-left: 5px;
    font-size: 18px;
}

.del {
    float: left;
    width: 10%;
    height: 40px;
    line-height: 40px;
}

.icon {
    margin-left: 40px;
    font-size: 25px;
}

.footer {
    background-color: #dadada;
    display: flex;
    width: 100%;
    height: 60px;
    position: fixed;
    z-index: 1000;
    bottom: 0;
    left: 0;
}

.f1 {
    margin-left: 10%;
    margin-top: 18px;
}

.f1 span {
    color: red;
    font-size: 18px;
}

.f2 {
    margin-left: 12%;
    height: 40px;
    width: 80px;
    margin-top: 10px;
    line-height: 40px;
    text-align: center;
    border-radius: 5px;
    color: #fff;
    background-color: #FE4D01;
}

你已经有了计算每个项目的小计。你只要把所有小计相加,得到总计就好了。

function getAll() {
    var count = 0;
    var sum = 0;
    $('.mrb').each(function(index, item) {
        count += Number($(item).children('input').val());
    });

    // 遍历所有小计,累加到 sum 变量中
    $('.total').each(function(index, item) {
        sum += parseFloat($(item).text().slice(1));
    });

    $('.f1 em').text(count);
    // 设置总计
    $('.iae').text(sum.toFixed(2));
}
收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

1. JK下载官网所有资源来源于开发团队,加入会员即可下载使用!如有问题请联系右下角在线客服!
2. JK下载官方保障所有软件都通过人工亲测,为每位会员用户提供安全可靠的应用软件、游戏资源下载及程序开发服务。
3. JK开发团队针对会员诉求,历经多年拥有现今开发成果, 每款应用程序上线前都经过人工测试无误后提供安装使用,只为会员提供安全原创的应用。
4. PC/移动端应用下载后如遇安装使用问题请联系右下角在线客服或提交工单,一对一指导解决疑难。

JK软件下载官网 技术分享 jquery计算总计,没有小计直接计算怎么写? https://www.jkxiazai.com/1582.html

JK软件应用商店是经过官方安全认证,保障正版软件平台

相关资源

官方客服团队

为您解决烦忧 - 24小时在线 专业服务