国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

node.js|vue.js|jquery|angularjs|React|json|js教程|

服務(wù)器之家 - 編程語言 - JavaScript - jquery - jQuery實現(xiàn)購物車全功能

jQuery實現(xiàn)購物車全功能

2021-12-29 16:18劉劉劉code jquery

這篇文章主要為大家詳細(xì)介紹了jQuery實現(xiàn)購物車全功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了jQuery實現(xiàn)購物車全功能的具體代碼,供大家參考,具體內(nèi)容如下

效果圖:

jQuery實現(xiàn)購物車全功能

HTML&&CSS:

?
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<!DOCTYPE html>
<html lang="en">
 
<head>
 <meta charset="UTF-8">
 <meta name="viewport" content="width=device-width, initial-scale=1.0">
 <title>Document</title>
 <script src="../jquery-3.4.1.min.js"></script>
 <style>
  * {
   margin: 0;
   padding: 0;
  }
  
  .tab {
   width: 500px;
   border-collapse: collapse;
   margin: 0 auto;
  }
  
  .tab td,
  th {
   border: 1px solid #000;
  }
  
  .tab .num {
   width: 20px;
  }
  
  .tab .add,
  .sub {
   width: 20px;
  }
  
  .current {
   background-color: pink;
  }
 </style>
</head>
 
<body>
 <table class="tab">
  <thead>
   <th>全選 <input type="checkbox" name="" value="" class="checkAll">
    <input type="checkbox" name="" value="" class="checkAll">
   </th>
   <th>商品名稱</th>
   <th>單價</th>
   <th>數(shù)量</th>
   <th>小計</th>
   <th>操作</th>
  </thead>
  <tbody>
   <tr>
    <td><input type="checkbox" class="ed" /></td>
    <td>電腦</td>
    <td class="price">¥200.20</td>
    <td>
     <button type="button" class="sub">-</button>
     <input type="text" name="" value="1" class="num">
     <button type="button" class="add">+</button>
    </td>
    <td class="small_total">¥200.20</td>
    <td class="delete">刪除</td>
   </tr>
   <tr>
    <td><input type="checkbox" class="ed" /></td>
    <td>手機</td>
    <td class="price">¥100.30</td>
    <td>
     <button type="button" class="sub">-</button>
     <input type="text" name="" value="1" class="num">
     <button type="button" class="add">+</button>
    </td>
    <td class="small_total">¥100.30</td>
    <td class="delete">刪除</td>
   </tr>
   <tr>
    <td><input type="checkbox" class="ed" /></td>
    <td>空調(diào)</td>
    <td class="price">¥1000.99</td>
    <td>
     <button type="button" class="sub">-</button>
     <input type="text" name="" value="1" class="num">
     <button type="button" class="add">+</button>
    </td>
    <td class="small_total">¥1000.99</td>
    <td class="delete">刪除</td>
   </tr>
  </tbody>
 </table>
 <div>
  <span>已選<span style="color: red;" class="num_sum">1</span>件商品</span>
  <span>總計:</span>
  <span class="sum" style="color: red;">0</span>
  <div><span style="color: red;" class="delSome">刪除選中商品</span>
   <span style="color: red;" class="delAll">清空購物車</span>
  </div>
 </div>
 </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
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
//里面三個小的復(fù)選按鈕選中狀態(tài)跟著 全選按鈕走
//因為checked是復(fù)選框的固有屬性,此時利用prop()獲取和設(shè)置該屬性
$(function() {
   getSum();
   $(".checkAll").change(function() {
     // console.log($(this).prop("checked"));//全選按鈕的狀態(tài)
     $(".ed,.checkAll").prop("checked", $(this).prop("checked"));
     getSum();
     if ($(".ed,.checkAll").prop("checked")) {
      //如果全選,讓所有商品添加類名(背景顏色)
      $(".tab tbody").children().addClass("current");
     } else {
      $(".tab tbody").children().removeClass("current");
     }
    })
    //如果所有小按鈕的個數(shù)都被選了,全選按鈕就選上,如果小按鈕沒有被選上,則全選按鈕就不選上
    //:checked選擇器,查找本選中的表單元素
   $(".ed").change(function() {
    // console.log($(".ed:checked").length);//小復(fù)選框選中的個數(shù)
    // console.log($(".ed").length);
    //console.log($(this).prop("checked"));
    if ($(".ed:checked").length === $(".ed").length) {
     $(".checkAll").prop("checked", true);
    } else {
     $(".checkAll").prop("checked", false);
    }
    getSum();
    if ($(this).prop("checked")) {
     $(this).parents("tr").addClass("current");
    } else {
     $(this).parents("tr").removeClass("current");
    }
   })
 
   $(".add").click(function() {
    let n = parseInt($(this).siblings(".num").val());
    //console.log(n);
    n++;
    $(this).siblings(".num").val(n);
    let price = $(this).parent().siblings(".price").html();
    price = price.substr(1);
    //console.log(price);
    $(this).parent().siblings(".small_total").text("¥" + (n * price).toFixed(2));
    getSum();
   })
   $(".sub").click(function() {
     let n = parseInt($(this).siblings(".num").val());
     //console.log(n);
     if (n === 1) {
      return false;
     }
     n--;
     $(this).siblings(".num").val(n);
     let price = $(this).parent().siblings(".price").html();
     price = price.substr(1);
     //console.log(price);
     $(this).parent().siblings(".small_total").text("¥" + (n * price).toFixed(2));
     getSum();
    })
    //用戶也可以直接修改表單num里面的值(小bug),同樣計算小計
   $(".num").change(function() {
    let n = $(this).val();
    let price = $(this).parent().siblings(".price").html();
    price = price.substr(1);
    $(this).parent().siblings(".small_total").text("¥" + (n * price).toFixed(2));
    getSum();
   })
 
   function getSum() {
    let count = 0; //計算總件數(shù)
    let money = 0; //計算總價錢
    $(".num").each(function(index) {
     if ($(".ed").eq(index).prop("checked") == true) {
      count += parseInt($(".num").eq(index).val());
      money += parseFloat($(".small_total").eq(index).text().substr(1));
     }
    })
    $(".num_sum").html(count);
    $(".sum").html(money.toFixed(2));
   }
 
   //刪除商品模塊
   //點擊刪除之后一定是刪除當(dāng)前的商品,所以從$(this)出發(fā)
   $(".delete").click(function() {
     //刪除的是當(dāng)前的商品
     $(this).parent().remove();
     $(".ed").change();
     getSum();
     clearCheckAll();
    })
    //刪除選定的商品:小的復(fù)選框如果選中就刪除對應(yīng)的商品
   $(".delSome").click(function() {
     //刪除的是選中的商品
     $(".ed:checked").parent().parent().remove();
     getSum();
     clearCheckAll();
    })
    //清空購物車
   $(".delAll").click(function() {
    $(".tab tbody").empty();
    getSum();
    clearCheckAll();
   })
 
   function clearCheckAll() {
    if ($(".tab tbody")[0].innerText == '') {
     $(".checkAll").prop("checked", false);
    }
   }
})

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

原文鏈接:https://blog.csdn.net/weixin_47339511/article/details/112360362

延伸 · 閱讀

精彩推薦
  • jqueryjquery實現(xiàn)穿梭框功能

    jquery實現(xiàn)穿梭框功能

    這篇文章主要為大家詳細(xì)介紹了jquery實現(xiàn)穿梭框功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下...

    陳濤輝8412022-01-04
  • jqueryjQuery使用hide()、toggle()函數(shù)實現(xiàn)相機品牌展示隱藏功能

    jQuery使用hide()、toggle()函數(shù)實現(xiàn)相機品牌展示隱藏功能

    這篇文章主要介紹了jQuery使用hide()、toggle()函數(shù)實現(xiàn)相機品牌展示隱藏功能,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考...

    Schieber11822022-01-11
  • jqueryjQuery實現(xiàn)鼠標(biāo)拖動圖片功能

    jQuery實現(xiàn)鼠標(biāo)拖動圖片功能

    這篇文章主要介紹了jQuery實現(xiàn)鼠標(biāo)拖動圖片功能,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以...

    lucascube5812022-02-10
  • jqueryjquery插件實現(xiàn)圖片懸浮

    jquery插件實現(xiàn)圖片懸浮

    這篇文章主要為大家詳細(xì)介紹了jquery插件實現(xiàn)圖片懸浮,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下...

    阿飛超努力5802022-03-03
  • jqueryjquery插件實現(xiàn)搜索歷史

    jquery插件實現(xiàn)搜索歷史

    這篇文章主要為大家詳細(xì)介紹了jquery插件實現(xiàn)搜索歷史,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下...

    阿飛超努力8462022-03-09
  • jqueryjQuery是用來干什么的 jquery其實就是一個js框架

    jQuery是用來干什么的 jquery其實就是一個js框架

    jQuery是一bai個簡潔而快速的JavaScript庫,可用于du簡化zhi事件處理,HTML文檔遍歷,Ajax交互和dao動畫,以更快速開發(fā)網(wǎng)站...

    jQuery教程網(wǎng)8842022-01-17
  • jqueryjQuery實現(xiàn)本地存儲

    jQuery實現(xiàn)本地存儲

    這篇文章主要為大家詳細(xì)介紹了jQuery實現(xiàn)本地存儲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下...

    李大璟10682021-12-16
  • jqueryjQuery treeview樹形結(jié)構(gòu)應(yīng)用

    jQuery treeview樹形結(jié)構(gòu)應(yīng)用

    這篇文章主要為大家詳細(xì)介紹了jQuery treeview樹形結(jié)構(gòu)應(yīng)用,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下...

    Lqq77s9342022-02-20
主站蜘蛛池模板: 蜜桃精品久久久久久久免费影院 | 中文字幕视频 | 黑人巨大精品欧美一区免费视频 | jizzxxx日本 | 日本中文字幕在线播放 | 在线观看国产一区视频 | 精品国产欧美一区二区三区成人 | 欧美一区视频 | 亚洲精品在线视频 | 免费观看国产精品 | 亚洲一区二区中文字幕 | 色视频在线播放 | 这里只有久久精品 | 黄色国产在线看 | 亚洲午夜在线 | 99在线精品视频 | 精品视频在线视频 | 亚洲视频三区 | 视频1区 | 久久99深爱久久99精品 | 中日韩一线二线三线视频 | 色九九 | 午夜电影av| 日韩精品一区二区三区在线播放 | 欧美日韩电影一区 | 欧美日韩精品一区二区三区 | 91免费视频观看 | 国产欧美日韩在线观看 | 成人激情在线观看 | 精品亚洲永久免费精品 | 亚洲精品乱码久久久久久金桔影视 | 狠狠干美女 | 隔壁老王国产在线精品 | 日韩不卡一二三 | ririsao久久精品一区 | 欧美色视频在线观看 | 天天澡天天狠天天天做 | 亚洲成人av一区二区三区 | 国产精品久久亚洲 | 国产视频网 | av一级久久|