$(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)); }