基于JQuery的購物車添加刪除以及結(jié)算功能示例
前段時(shí)間了解到購物車結(jié)算算是一個(gè)難點(diǎn)部分,在網(wǎng)上也找了一些,但是網(wǎng)上除了插件之外,就是一些半成品,比如一部分只有添加刪除效果,另一部分只有結(jié)算功能,很少見到整合在一起的購物車效果,因此自己寫了一個(gè),方便大家查看
(添加效果沒有飛入,實(shí)在懶得寫動(dòng)畫效果了,湊合看吧)
HTML部分
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title></title> <link rel="stylesheet" href="css/index.css" rel="external nofollow" > <script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js" type="text/javascript" charset="utf-8"></script> <script src="js/index.js" type="text/javascript" charset="utf-8" async defer></script> </head> <body> <div id="banner"></div> <div id="container"> <ul> <li num="1"><img src="images/1.jpg"/><span class="things_name">2014年春季爆品A<p>$<i>10</i><span class="buy">點(diǎn)擊購買</p></li> <li num="2"><img src="images/2.jpg"/><span class="things_name">2014年春季爆品B<p>$<i>20</i><span class="buy">點(diǎn)擊購買</p></li> <li num="3"><img src="images/3.jpg"/><span class="things_name">2014年春季爆品C<p>$<i>30</i><span class="buy">點(diǎn)擊購買</p></li> <li num="4"><img src="images/4.jpg"/><span class="things_name">2014年春季爆品D<p>$<i>40</i><span class="buy">點(diǎn)擊購買</p></li> </ul> </div> <div id="carlist"> <div class="car"> <div> <span class="carLogo"><span><img src="images/car.png"/> <span class="txt">購<br />物<br />車 </div> </div> <div class="list"> <!--此處添加動(dòng)態(tài)元素--> <div class="total"> <span>總價(jià):<span>0元 </div> </div> </div> <script type="text/javascript"> function view(){ return{ w:document.documentElement.clientWidth, h:document.documentElement.clientHeight }; } document.body.style.height=view().h+"px"; </script> </body> </html>
JS部分
$(function(){ var mark=0; $(".car").on("click",function(){ if(mark==0){ $("#carlist").animate({marginRight:"0px"},500) mark=1; }else{ $("#carlist").animate({marginRight:"-260px"},500) mark=0 } }) //點(diǎn)擊購買按鈕添加至購物車 var buyButton=$(".buy"); buyButton.on("click",BuyClick) function BuyClick(){ var thingsName=$(this).parents("li").find(".things_name").text(); var thingsPrice=$(this).parent().find("i").text(); var thingsImage=$(this).parents("li").find("img").attr("src"); var kNum=$(this).parents("li").attr("num") var Geshu=1; $(this).off("click").text("已經(jīng)添加至購物車"); $(".list").append('<div class="select things" num='+kNum+'><img src='+thingsImage+'/><p class="name">'+thingsName+'</p><p class="price">$<i>'+thingsPrice+'</i></p><ul class="caozuo"><li class="zengjian"><span class="minus">-<span>1<span class="add">+</li><li class="del">刪除</li></ul></div>'); countTotalPrice(); totalGeshu(); //點(diǎn)擊加號(hào)添加商品個(gè)數(shù) $(".add").off("click").on("click",function(){ Geshu=parseInt($(this).parent().find("span:nth-of-type(2)").text()) Geshu++ $(this).parent().find("span:nth-of-type(2)").text(Geshu) countTotalPrice(); totalGeshu(); }) //動(dòng)態(tài)生成的元素點(diǎn)擊減號(hào)減少商品個(gè)數(shù) $(".minus").off("click").on("click",function(){ Geshu=parseInt($(this).parent().find("span:nth-of-type(2)").text()); if(Geshu>1){ Geshu--; $(this).parent().find("span:nth-of-type(2)").text(Geshu) }else{ Geshu==1; } countTotalPrice(); totalGeshu(); }) //刪除購物車內(nèi)的商品 var del=$(".del"); del.each(function(){ $(this).off("click").on("click",function(){ var delName=$(this).parents(".things").find(".name").text(); $(this).parents(".things").remove(); countTotalPrice(); totalGeshu(); var oldBtn=$("#container ul li").find("span:contains("+delName+")").parents("li").find(".buy") oldBtn.on("click",BuyClick).text("點(diǎn)擊購買") }) }) //計(jì)算總價(jià)函數(shù) function countTotalPrice(){ var totalPrice=0,listThings=$(".list").find(".things"); for (var i=0;i<listThings.length;i++) { var this_geshu=parseInt(listThings.eq(i).find(".zengjian span:nth-of-type(2)").text()); var this_price=parseInt(listThings.eq(i).find(".price i").text()); totalPrice+=this_geshu*this_price; } $(".total span").eq(1).text(totalPrice); totalGeshu(); } //購物車上的商品總數(shù) function totalGeshu(){ var listThings=$(".list").find(".things"); if (listThings.length>0) { var totalGeshu=0; listThings.each(function(){ var this_geshu=parseInt($(this).find(".zengjian span:nth-of-type(2)").text()); totalGeshu+=this_geshu; }) $(".carLogo span").html(totalGeshu) } else{ $(".carLogo span").css("display","none") } } } })
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- jQuery實(shí)現(xiàn)移動(dòng)端手機(jī)商城購物車功能
- jQuery實(shí)現(xiàn)購物車多物品數(shù)量的加減+總價(jià)計(jì)算
- jQuery實(shí)現(xiàn)加入購物車飛入動(dòng)畫效果
- JQuery實(shí)現(xiàn)的購物車功能(可以減少或者添加商品并自動(dòng)計(jì)算價(jià)格)
- 純jquery實(shí)現(xiàn)模仿淘寶購物車結(jié)算
- 基于JQuery實(shí)現(xiàn)的類似購物商城的購物車
- jQuery+HTML5加入購物車代碼分享
- jQuery實(shí)現(xiàn)類似淘寶購物車全選狀態(tài)示例
- jquery購物車結(jié)算功能實(shí)現(xiàn)方法
- jQuery仿天貓實(shí)現(xiàn)超炫的加入購物車
- jquery制作的移動(dòng)端購物車效果完整示例
相關(guān)文章
jQuery在IE下使用未閉合的xml代碼創(chuàng)建元素時(shí)的Bug介紹
這個(gè)偏門Bug是我在更新phZoom 1.29后發(fā)現(xiàn)的, 我先將之重現(xiàn)一下2012-01-01基于jquery的滾動(dòng)鼠標(biāo)放大縮小圖片效果
基于jquery的滾動(dòng)鼠標(biāo)放大縮小圖片效果,需要的朋友可以參考下。2011-10-10jquery精度計(jì)算代碼 jquery指定精確小數(shù)位
這篇文章主要為大家詳細(xì)介紹了jquery精度計(jì)算代碼,jquery指定精確小數(shù)位,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-02-02Jquery 實(shí)現(xiàn)Tab效果 思路是js思路
Jquery 實(shí)現(xiàn)Tab效果,思路就是普通的用js的思路控制的,腳本之家發(fā)布過更精簡的代碼,這個(gè)比較適合一直用js開始學(xué)用jquery控制的朋友一個(gè)參考。2010-03-03用JQuery調(diào)用Session的實(shí)現(xiàn)代碼
用JQuery調(diào)用Session的實(shí)現(xiàn)代碼,需要的朋友可以參考下。2010-10-10將form表單通過ajax實(shí)現(xiàn)無刷新提交的簡單實(shí)例
下面小編就為大家?guī)硪黄獙orm表單通過ajax實(shí)現(xiàn)無刷新提交的簡單實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-10-10