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

HTML代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<!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代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/****点击增加按钮****/
$( '.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代码,最终页面截图如下:

wKioL1gIYA6g7MTbAAEULD6Wu_A791.png

本文转自   frwupeng517   51CTO博客,原文链接:http://blog.51cto.com/dapengtalk/1863852

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
jQuery 是一个流行的 JavaScript 框架,它简化了 HTML 文档遍历、事件处理、动画和Ajax交互等任务。使用 jQuery 实现购物车的基本功能,通常会涉及以下几个步骤: 1. **添加商品到购物车**: - 创建一个或多个HTML元素表示商品,包含名称、价格和“加入购物车”按钮。 - 给按钮添加点击事件,当点击时,使用$.ajax调用服务器端API将商品信息添加到购物车中。 ```javascript $(".add-to-cart").click(function() { var item = $(this).data("item"); $.ajax({ url: "add-to-cart.php", type: "POST", data: { item: item }, success: function(response) { // 更新购物车显示 }, error: function(xhr, status, error) { console.error(error); } }); }); ``` 2. **显示购物车内容**: - 可以通过查询服务器获取当前用户的购物车信息,并使用jQuery动态渲染到页面上。 ```javascript function fetchCart() { $.ajax({ url: "get-cart.php", success: function(cartItems) { $("#cart").html(cartItems); }, error: function(xhr, status, error) { console.error(error); } }); } // 初始加载购物车,或者在用户登录后刷新购物车数据 fetchCart(); ``` 3. **删除购物车中的商品**: - 为购物车列表中的每个商品项添加删除按钮,同样使用$.ajax发送请求到服务器。 ```javascript $(".delete-from-cart").click(function() { var itemId = $(this).data("itemId"); $.ajax({ url: "delete-item.php", type: "POST", data: { itemId: itemId }, success: function(response) { // 更新购物车显示 fetchCart(); }, error: function(xhr, status, error) { console.error(error); } }); }); ``` 4. **结算与计价**: - 需要计算购物车总价,可能需要实时更新或在用户完成购物车操作时执行。 ```javascript function calculateTotal() { var total = 0; $("#cart .item-price").each(function() { total += parseInt($(this).text()); }); $("#total-price").html(total); } // 在商品数量变化或添加/删除商品后更新总价格 $("#cart").on("change", ".item-quantity", function() { calculateTotal(); }); // 在用户添加商品或删除后重新计算 fetchCart().then(calculateTotal); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值