jQuery實(shí)現(xiàn)購物車全功能
更新時(shí)間:2021年01月11日 07:48:39 作者:劉劉劉code
這篇文章主要為大家詳細(xì)介紹了jQuery實(shí)現(xiàn)購物車全功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
本文實(shí)例為大家分享了jQuery實(shí)現(xiàn)購物車全功能的具體代碼,供大家參考,具體內(nèi)容如下
效果圖:

HTML&&CSS:
<!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>單價(jià)</th>
<th>數(shù)量</th>
<th>小計(jì)</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>手機(jī)</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>總計(jì):</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:
//里面三個(gè)小的復(fù)選按鈕選中狀態(tài)跟著 全選按鈕走
//因?yàn)閏hecked是復(fù)選框的固有屬性,此時(shí)利用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");
}
})
//如果所有小按鈕的個(gè)數(shù)都被選了,全選按鈕就選上,如果小按鈕沒有被選上,則全選按鈕就不選上
//:checked選擇器,查找本選中的表單元素
$(".ed").change(function() {
// console.log($(".ed:checked").length);//小復(fù)選框選中的個(gè)數(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),同樣計(jì)算小計(jì)
$(".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; //計(jì)算總件數(shù)
let money = 0; //計(jì)算總價(jià)錢
$(".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));
}
//刪除商品模塊
//點(diǎn)擊刪除之后一定是刪除當(dāng)前的商品,所以從$(this)出發(fā)
$(".delete").click(function() {
//刪除的是當(dāng)前的商品
$(this).parent().remove();
$(".ed").change();
getSum();
clearCheckAll();
})
//刪除選定的商品:小的復(fù)選框如果選中就刪除對(duì)應(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)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
jquery實(shí)現(xiàn)進(jìn)度條狀態(tài)展示
這篇文章主要為大家詳細(xì)介紹了jquery實(shí)現(xiàn)進(jìn)度條狀態(tài)展示,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-03-03
Jquery定義對(duì)象(閉包)與擴(kuò)展對(duì)象成員的方法
這篇文章介紹了Jquery定義對(duì)象(閉包)與擴(kuò)展對(duì)象成員的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-06-06
jQuery CSS3自定義美化Checkbox實(shí)現(xiàn)代碼
這篇文章主要為大家詳細(xì)介紹了jQuery CSS3自定義美化Checkbox實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-05-05
a標(biāo)簽跳轉(zhuǎn)到指定div,jquery添加和移除class屬性的實(shí)現(xiàn)方法
下面小編就為大家?guī)硪黄猘標(biāo)簽跳轉(zhuǎn)到指定div,jquery添加和移除class屬性的實(shí)現(xiàn)方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-10-10

