记录一下项目中遇到的计算购物车商品数量和总价的jQuery代码,重点在于选择器以及.text()命令的使用。

先上效果图,点击加减,商品数量以及总价会发生相应变化。

html代码:

<div class="addGoods">
<div class="goods_list">
<ul>
<li><img class="goods_img" src="../img/goods1.png"><p>木林森男鞋</p>
<div class="goods_bottom">¥<span class="goods_price">198.00</span><div class="num"><img class="minus" src="../img/minus.png" /><span>0</span><img class="plus" src="../img/plus.png"></div></div></li>
<li><img class="goods_img" src="../img/goods2.png"><p>Meizu/魅族 魅蓝E2</p>
<div class="goods_bottom">¥<span class="goods_price">1200.00</span><div class="num"><img class="minus" src="../img/minus.png" /><span>0</span><img class="plus" src="../img/plus.png"></div></div></li>
<li><img class="goods_img" src="../img/goods3.png"><p>清风傲骨茶仓</p>
<div class="goods_bottom">¥<span class="goods_price">254.00</span><div class="num"><img class="minus" src="../img/minus.png" /><span>0</span><img class="plus" src="../img/plus.png"></div></div></li>
<li><img class="goods_img" src="../img/goods4.png"><p>农家小米糯米锅巴</p>
<div class="goods_bottom">¥<span class="goods_price">22.90</span><div class="num"><img class="minus" src="../img/minus.png" /><span>0</span><img class="plus" src="../img/plus.png"></div></div></li>
<li><img class="goods_img" src="../img/goods5.png"><p>安德玛 UA男子 Curry3</p>
<div class="goods_bottom">¥<span class="goods_price">7.00</span><div class="num"><img class="minus" src="../img/minus.png" /><span>0</span><img class="plus" src="../img/plus.png"></div></div></li>
</ul>
<div class="pay">共计<span class="totalNum">0</span>件
<p>总价¥<span class="totalPrice">0</span></p><button type="submit">去付款</button></div>
</div>
</div>

列表部分CSS代码:

.goods_list ul li{
background-color: #FFF;
border-bottom: 1px solid #CCC;
height: 260px;
padding: 40px;
}
.goods_img{
float: left;
border: 1px solid #CCC;
margin-right: 20px;
width: 180px;
height: 180px;
}
.goods_bottom{
padding-top: 52px;
}
.goods_bottom .price{
color: #F02028;
}
.goods_list .num{
float: right;
}
.goods_list .num span{
padding: 0 30px;
}
.pay {
padding-left: 30px;
background-color: #FFF;
width: 100%;
height: 166px;
position: fixed;bottom:;
}
.pay .totalNum{
color: #F02028;
}
.pay p{
color: #F02028;
}
.pay button{
position: fixed;bottom:;right:;
width: 300px;
height: 166px;
background-color: #FF3334;
color: #FFF;
font-size: 45px;
}

点击按钮后,需要改变的地方有三处,列表里加减中间的数字,底部的totalNum和totalPrice。

jQuery代码:

$(document).ready(function(){
//点击增加按钮触发事件
$(".plus").click(function(){
var num = $(this).parent().children("span");
//单品数量增加
num.text(parseInt(num.text())+1);
//商品总数增加
var totalNum = parseInt($(".totalNum").text());
totalNum++
$(".totalNum").text(totalNum);
//计算总价
var goods_price = parseInt($(this).parent().parent().children(".goods_price").text());
$(".totalPrice").text(parseInt($(".totalPrice").text())+goods_price);
});

//点击减少按钮触发事件
$(".minus").click(function(){
var num = $(this).parent().children("span");
if(parseInt(num.text())){
num.text(parseInt(num.text())-1);
var totalNum = parseInt($(".totalNum").text());
totalNum--
$(".totalNum").text(totalNum);
var goods_price = parseInt($(this).parent().parent().children(".goods_price").text());
$(".totalPrice").text(parseInt($(".totalPrice").text())-goods_price);
} else{
num.text("0");
}
});
});

值得注意的有以下几点:

1. .parent()函数用于选取每个匹配元素的父元素,并以jQuery对象的形式返回;.parent()函数用于选取每个匹配元素的子元素, .find 和 .children() 方法类似,不过后者只沿着 DOM 树向下遍历单一层级

2. text()函数用于返回或设置当前jQuery对象所匹配的DOM元素内的text内容,且该值为字符串类型,所以要用parseInt()转换为整数类型才可以进行计算。

3. 需要添加单品数量小于0时的处理,防止出现负数。

PS:选择器用得过于繁琐,存在改进空间。

jQuery实现购物车商品数量及总价的计算的更多相关文章

  1. Flutter实战视频-移动电商-63.购物车_详细页显示购物车商品数量

    63.购物车_详细页显示购物车商品数量 购物车的图标嵌套在statck组件里面 外层套了一个stack组件 数量我们需要用Provide 返回一个container来做样式 气泡效果,中间是个数字外面 ...

  2. 【SSH网上商城项目实战28】使用Ajax技术局部更新商品数量和总价

         转自: https://blog.csdn.net/eson_15/article/details/51487323 昨天把项目部署了一下,玩了玩,今天完善了一下购物车中修改商品数量就能局部 ...

  3. 【ecshop】调用购物车商品数量

    1 打开 includes/lib_insert.php 在最后位置添加如下代码: /** * 调用购物车商品数目 */ function insert_cart_mes_num() { $sql = ...

  4. django-获取购物车商品数量-redis

    视图函数views.py中 from django_redis import get_redis_connection # 连接redis class IndexView(View): '''首页'' ...

  5. 利用 jQuery 操作页面元素的方法,实现电商网站购物车页面商品数量的增加和减少操作,要求单项价格和总价随着数量的改变而改变

    查看本章节 查看作业目录 需求说明: 利用 jQuery 操作页面元素的方法,实现电商网站购物车页面商品数量的增加和减少操作,要求单项价格和总价随着数量的改变而改变 当用户单击"+" ...

  6. 069——VUE中vuex之使用getters高效获取购物车商品总价

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. jQuery实现购物车多物品数量的加减+总价计算

        <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3. ...

  8. jQuery实现购物车多物品数量的加减+总价+删除计算

    <?php session_start(); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//E ...

  9. 仿淘宝购物车demo---增加和减少商品数量

    在上一篇博客中,小编简单的介绍了如何使用listview来实现购物车,但是仅仅是简单的实现了列表的功能,随之而来一个新的问题,买商品的时候,我们可能不止想买一件商品,想买多个,或许有因为某种原因点错了 ...

随机推荐

  1. 华为Mate8手机优化技巧

    我的华为Mate8手机手机存储(手机内存)32G, 运行内存3G,2016年买的手机,两样数值都偏小,导致使用手机的时候经常卡顿,一天来回清理垃圾.清理内存很多次,要运行微信.QQ等占用运行内存较多的 ...

  2. JS的函数和对象二

    复习 递归,在函数内部调用自身  return 匿名函数  function(){   } 创建函数,函数表达式  var fn=function(){   } 自调用   (function(){ ...

  3. 00008 - layui 表单验证,需要验证,但非必输

    当使用layui的验证规则,比如 手机, <input type="text" name="userName" lay-verify="phon ...

  4. select 下拉框样式修改 option文字居右

    select { direction: rtl; /*Chrome和Firefox里面的边框是不一样的,所以复写了一下*/ border: solid 1px #000; /*很关键:将默认的sele ...

  5. E. Physical Education Lessons 动态开辟线段树区间更新

    E. Physical Education Lessons time limit per test 1 second memory limit per test 256 megabytes input ...

  6. codeforce 436 D贪心思维题Make a Permutation!

    D. Make a Permutation! time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

  7. OpenStack知识点详解

    一:云计算     一.起源 1. 云计算这个概念首次在2006年8月的搜索引擎会议上提出,成为了继互联网.计算机后信息时代的又一种革新(互联网第三次革命). 2. 云计算的核心是将资源协调在一起,使 ...

  8. 使用 IdentityService4 集成小程序登录一种尝试

    1 场景介绍 主要业务是通过 App 承载,在 App 中可以注册和登录,为了更好的发展业务引入了微信小程序,于是如何让这两个入口的用户互通便成了需要解决的问题. 看了一下其它 App 大致地思路是两 ...

  9. 发光加载环动画-纯CSS动画效果-如何创建CSS3旋转预加载器(参照https://www.bilibili.com/video/BV1V4411C7z5?from=search&seid=9741275927942612817)

    //css部分 body{ margin:; padding:; background: #262626; } .ring{ position: absolute; top:50%; left: 50 ...

  10. 读Pyqt4教程,带你入门Pyqt4 _006

    窗口组件是应用程序的基本构建块.PyQt4编程工具包拥有范围广泛的各种窗口组件.按钮.选择框.滑块.列表框等等,程序员工作所需要的一切.在教程的这部分中,我们将介绍一些有用的窗口组件. QCheckB ...