jquery實(shí)現(xiàn)購物車基本功能
購物車?yán)锏墓δ軣o非是商品數(shù)量的加減、商品刪除、全選反選等操作,其實(shí)現(xiàn)過程如下所示:
1.html代碼:
<body> <div class="empty"> 購物車空空如也,<a href="javascript:void(0);" >快去選購吧</a> </div> <table border="2px solid #ccc" id="table"> <thead> <th> <input type="checkbox" class="checkOnly" style="vertical-align:middle;margin-right:20px;">全選 </th> <th>序號</th> <th>商品名稱</th> <th>數(shù)量</th> <th>單價(jià)</th> <th>小計(jì)</th> <th>操作</th> </thead> <tbody> <tr> <td> <input type="checkbox" class="check"> </td> <td class="num">1</td> <td>烤煎餅</td> <td> <span> <input type="button" value="-" class="reduces"> <span class="span">1</span> <input type="button" value="+" class="adds"> </span> </td> <td>單價(jià): <span class="price">2</span> </td> <td> 小計(jì): <span class="prices">2</span> </td> <td> <a href="#" class="del">刪除</a> </td> </tr> <tr> <td> <input type="checkbox" class="check"> </td> <td class="num">2</td> <td>珍珠奶茶</td> <td> <span> <input type="button" value="-" class="reduces"> <span class="span">1</span> <input type="button" value="+" class="adds"> </span> </td> <td>單價(jià): <span class="price">4</span> </td> <td> 小計(jì): <span class="prices">4</span> </td> <td> <a href="#" class="del">刪除</a> </td> </tr> <tr> <td> <input type="checkbox" class="check"> </td> <td class="num">3</td> <td>水煮魚</td> <td> <span> <input type="button" value="-" class="reduces"> <span class="span">1</span> <input type="button" value="+" class="adds"> </span> </td> <td>單價(jià): <span class="price">20</span> </td> <td> 小計(jì): <span class="prices">20</span> </td> <td> <a href="#" class="del">刪除</a> </td> </tr> <tr> <td> <input type="checkbox" class="check"> </td> <td class="num">4</td> <td>蛋糕</td> <td> <span> <input type="button" value="-" class="reduces"> <span class="span">1</span> <input type="button" value="+" class="adds"> </span> </td> <td>單價(jià): <span class="price">50</span> </td> <td> 小計(jì): <span class="prices">50</span> </td> <td> <a href="#" class="del">刪除</a> </td> </tr> <tr> <td> <input type="checkbox" class="check"> </td> <td class="num">5</td> <td>土豆片</td> <td> <span> <input type="button" value="-" class="reduces"> <span class="span">1</span> <input type="button" value="+" class="adds"> </span> </td> <td>單價(jià): <span class="price">5</span> </td> <td> 小計(jì): <span class="prices">5</span> </td> <td> <a href="#" class="del">刪除</a> </td> </tr> <tr> <td> <input type="checkbox" class="check"> </td> <td class="num">6</td> <td>蛋黃派</td> <td> <span> <input type="button" value="-" class="reduces"> <span class="span">1</span> <input type="button" value="+" class="adds"> </span> </td> <td>單價(jià): <span class="price">5.5</span> </td> <td> 小計(jì): <span class="prices">5.5</span> </td> <td> <a href="#" class="del">刪除</a> </td> </tr> <tr> <td colspan="7" class="talast"> <span>商品一共 <span class="goods_num" style="color:red;font-size:20px;">0</span> 件; 共計(jì)花費(fèi) <span class="pricetal" style="color:red;font-size:20px;">0</span> 元; 其中最貴的商品單價(jià)是 <span class="pricest" style="color:red;font-size:20px;">0</span> 元</span> </td> </tr> </tbody> </table> </body>
2.css代碼:
<style type="text/css">
table {
width: 1000px;
/* height: 300px; */
border-collapse: collapse;
table-layout: fixed;
text-align: center;
font-size: 18px;
margin: 0 auto;
}
a {
text-decoration: none;
color: black;
}
tr {
height: 50px;
}
.check {
width: 20px;
height: 20px;
}
.checkOnly {
width: 20px;
height: 20px;
}
.empty {
font-size: 25px;
position: fixed;
top: 45%;
left: 45%;
display: none;
}
.empty a {
color: pink;
}
.empty a:hover {
text-decoration: underline;
}
</style>
3.js代碼:
<script src="js/jquery-1.8.3.min.js"></script> //引入jquery
<script src="js/Allcheck.js"></script> //引入封裝好的全選反選插件
<script>
$(function () {
$(".adds").each(function () { //點(diǎn)擊增加的按鈕
$(this).click(function () {
//1.改變數(shù)量
var count = parseFloat($(this).parents("tr").find(".span").html());
count++;
$(this).parent("span").find(".span").html(count);
//2.改小計(jì)(先找到單價(jià)與數(shù)量,再相乘,最后放在小計(jì)(“.prices”)里)
var price = parseFloat($(this).parents("tr").find(".price").html());
var money = (price * count);
$(this).parents("tr").find(".prices").html(money);
//3.改總價(jià)
total();
countAll();//最后的總數(shù)量
compare();//選中復(fù)選框時(shí)比較商品單價(jià)中最高的
});
});
$(".reduces").each(function () {//點(diǎn)擊減少的按鈕
$(this).click(function () {
//1.改變數(shù)量
var count = parseFloat($(this).parents("tr").find(".span").html());
count--;
if (count < 1) { //當(dāng)數(shù)量減到1時(shí)不能再減
return;
}
$(this).parent("span").find(".span").html(count);
//2.改小計(jì)
var price = parseFloat($(this).parents("tr").find(".price").html());
var money = (price * count);
$(this).parents("tr").find(".prices").html(money);
total();
countAll();//最后的總數(shù)量
compare();//選中復(fù)選框時(shí)比較商品單價(jià)中最高的
});
});
//刪除
$(".del").each(function () {
$(this).click(function () {
let re = $(this).parents("tr"); //找到要刪除的行
if (confirm("你確定刪除嗎?")) {
re.remove();
}
total();
countAll(); //總數(shù)量
compare(); //最貴的
//刪除后重新排序號
for (let i = 0; i < $(".num").length; i++) {
$(".num")[i].innerHTML = i + 1;
}
//全都刪除時(shí)清空table(通過獲取tbody的高度來判斷)
let clear = $("tbody").height();
if (clear == 50) {
$("table").remove();
$(".empty").css({ //刪完時(shí)顯示"購物車為空"這個(gè)盒子
display: "block"
});
}
});
});
//合計(jì)
function total() {
let sum = 0;
$(".prices").each(function () {//先循環(huán)每個(gè)小計(jì)
if (($(this).parents("tr").find(".check")).prop("checked")) {//判斷復(fù)選框有沒有選中
sum += parseFloat($(this).html());
}
$(".pricetal").html(sum);
});
}
//總數(shù)量
function countAll() {
let counts = 0;
for(let i=0;i<$(".check").length;i++){
if($(".check")[i].checked==true){ //注意此塊用jquery不好實(shí)現(xiàn)
counts+=parseInt( $('.span')[i].innerHTML);
}
}
$(".goods_num")[0].innerHTML=counts;
}
//最貴商品比較
function compare() {
let temp = 0;
for (let i = 0; i < $(".price").length; i++) { //循環(huán)單價(jià)
if($(".check")[i].checked==true){
var temps = parseFloat($(".price")[i].innerHTML);
if (temps > temp) {
temp = temps;
}
}
};
$(".pricest").html(temp);
}
//全選插件(引入插件Allcheck.js)
$(".checkOnly").bindCheck($("#table :checkbox"));
//當(dāng)點(diǎn)擊復(fù)選框時(shí)調(diào)用total()
$(".check").each(function (){
$(this).click(function () {
let num = 0;
$(".check").each(function () {
if ($(this).prop("checked")) {
num++;
}
countAll();
total();
compare(); //最貴的
});
if(num == $(".check").length){//如果復(fù)選框的長度與num相等時(shí),全選那個(gè)按鈕也會被選中
$(".checkOnly")[0].checked = true;
compare(); //最貴的
countAll(); //總數(shù)量
total();
}else{
$(".checkOnly")[0].checked = false;
}
});
});
$(".checkOnly").click(function () {
total();
countAll(); //總數(shù)量
compare(); //最貴的
});
});
</script>
補(bǔ)充上面js代碼中用到的全選反選插件 \color{red}{補(bǔ)充上面js代碼中用到的全選反選插件}補(bǔ)充上面js代碼中用到的全選反選插件
//1、定義全選的插件
jQuery.fn.extend({
bindCheck:function($subCheckBox,$btnUncheck){
let $allCheckBox = this;
//1、給全選復(fù)選框綁定click事件
//this:是全選復(fù)選框(jQuery對象)
this.click(function(){
let isChecked = this.checked;
$subCheckBox.each(function(){
this.checked = isChecked;
});
});
//2、給反選
if(arguments.length==2){
$btnUncheck.click(function(){
$subCheckBox.each(function(){
this.checked = !this.checked;
});
reversCheck();
});
}
//3、給每個(gè)選擇項(xiàng)的復(fù)選框綁定事件
$subCheckBox.click(function(){
reversCheck();
});
function reversCheck(){
//1、判斷是否全部的復(fù)選框被選中
let isAllChecked = true;
$subCheckBox.each(function(){
if(!this.checked){
isAllChecked = false;
}
});
//2、處理全選復(fù)選框
$allCheckBox.attr("checked",isAllChecked);
}
}
});
效果如下圖所示:

以上就是一個(gè)功能比較完整的購物車,有問題或者建議歡迎指出。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- jQuery實(shí)現(xiàn)購物車多物品數(shù)量的加減+總價(jià)計(jì)算
- jQuery實(shí)現(xiàn)加入購物車飛入動畫效果
- JQuery實(shí)現(xiàn)的購物車功能(可以減少或者添加商品并自動計(jì)算價(jià)格)
- jQuery實(shí)現(xiàn)購物車計(jì)算價(jià)格功能的方法
- 純jquery實(shí)現(xiàn)模仿淘寶購物車結(jié)算
- 基于JQuery實(shí)現(xiàn)的類似購物商城的購物車
- jQuery實(shí)現(xiàn)購物車數(shù)字加減效果
- jQuery+HTML5加入購物車代碼分享
- jquery購物車結(jié)算功能實(shí)現(xiàn)方法
- jQuery實(shí)現(xiàn)購物車
相關(guān)文章
Jquery實(shí)現(xiàn)搜索框提示功能示例代碼
數(shù)據(jù)量很大的情況下使用Ajax去實(shí)現(xiàn)真的不合適,于是想采用Jquery來實(shí)現(xiàn)方法,具體的示例代碼如下,有需求的朋友可以參考下希望對大家有所幫助2013-08-08
jquery的ajax和getJson跨域獲取json數(shù)據(jù)的實(shí)現(xiàn)方法
本篇文章主要是對jquery的ajax和getJson跨域獲取json數(shù)據(jù)的實(shí)現(xiàn)方法進(jìn)行了介紹,需要的朋友可以過來參考下,希望對大家有所幫助2014-02-02
JQuery Highcharts 動態(tài)生成圖表的方法
動態(tài)圖表生成方法有很多,在接下來的文章中將為大家介紹下使用JQuery Highcharts是如何實(shí)現(xiàn)的2013-11-11
基于jQuery的試卷自動排版系統(tǒng)實(shí)現(xiàn)代碼
題目提干、選擇題的選項(xiàng)、說明文字可以包含多媒體信息(文字、圖片、列表、表格、視頻等等……)。2011-01-01
jQuery EasyUI API 中文文檔 - ValidateBox驗(yàn)證框
jQuery EasyUI API 中文文檔 - ValidateBox驗(yàn)證框,使用jQuery EasyUI的朋友可以參考下。2011-10-10
jQuery UI Dialog控件中的表單無法正常提交的解決方法
研究了頁面源碼后發(fā)現(xiàn),jQuery UI Dialog控件初始化時(shí)動態(tài)生成的HTML元素被添加到頁面的尾部、form元素的后面,而原始的Dialog模板部分(其內(nèi)包含表單元素)也被移到了 動態(tài)生成的HTML元素內(nèi)。2010-12-12

