jquery.cookie實(shí)現(xiàn)的客戶端購(gòu)物車操作實(shí)例
本文實(shí)例講述了jquery.cookie實(shí)現(xiàn)的客戶端購(gòu)物車操作。分享給大家供大家參考,具體如下:
//購(gòu)物車 var Cart = function () { this.Count = 0; this.Total = 0; this.Items = new Array(); }; //購(gòu)物車集合對(duì)象 var CartItem = function () { this.Id = 0; this.Name = ""; this.Count = 0; this.Price = 0; }; //購(gòu)物車操作 var CartHelper = function () { this.cookieName = "yxhCart"; this.Clear = function () { var cart = new Cart(); this.Save(cart); return cart; }; //向購(gòu)物車添加 this.Add = function (id, name, count, price) { var cart = this.Read(); var index = this.Find(id); //如果ID已存在,覆蓋數(shù)量 if (index > -1) { cart.Total -= (((cart.Items[index].Count * 100) * (cart.Items[index].Price * 100)) / 10000); cart.Items[index].Count = count; cart.Total += (((cart.Items[index].Count * 100) * (cart.Items[index].Price * 100)) / 10000); } else { var item = new CartItem(); item.Id = id; item.Name = name; item.Count = count; item.Price = price; cart.Items.push(item); cart.Count++; cart.Total += (((cart.Items[index].Count * 100) * (cart.Items[index].Price * 100)) / 10000); } this.Save(cart); return cart; }; //改變數(shù)量 this.Change = function (id, count) { var cart = this.Read(); var index = this.Find(id); cart.Items[index].Count = count; this.Save(cart); return cart; }; //移出購(gòu)物車 this.Del = function (id) { var cart = this.Read(); var index = this.Find(id); if (index > -1) { var item = cart.Items[index]; cart.Count--; cart.Total = cart.Total - (((item.Count * 100) * (item.Price * 100)) / 10000); cart.Items.splice(index, 1); this.Save(cart); } return cart; }; //根據(jù)ID查找 this.Find = function (id) { var cart = this.Read(); var index = -1; for (var i = 0; i < cart.Items.length; i++) { if (cart.Items[i].Id == id) { index = i; } } return index; }; //COOKIE操作 this.Save = function (cart) { var source = ""; for (var i = 0; i < cart.Items.length; i++) { if (source != "") { source += "|$|"; } source += this.ItemToString(cart.Items[i]); } $.cookie(this.cookieName, source); }; this.Read = function () { //讀取COOKIE中的集合 var source = $.cookie(this.cookieName); var cart = new Cart(); if (source == null || source == "") { return cart; } var arr = source.split("|$|"); cart.Count = arr.length; for (var i = 0; i < arr.length; i++) { var item = this.ItemToObject(arr[i]); cart.Items.push(item); cart.Total += (((item.Count * 100) * (item.Price * 100)) / 10000); } return cart; }; this.ItemToString = function (item) { return item.Id + "||" + escape(item.Name) + "||" + item.Count + "||" + item.Price; }; this.ItemToObject = function (str) { var arr = str.split('||'); var item = new CartItem(); item.Id = arr[0]; item.Name = unescape(arr[1]); item.Count = arr[2]; item.Price = arr[3]; return item; }; };
希望本文所述對(duì)大家jQuery程序設(shè)計(jì)有所幫助。
- jQuery通過(guò)寫入cookie實(shí)現(xiàn)更換網(wǎng)頁(yè)背景的方法
- Jquery操作cookie記住用戶名
- jquery中cookie用法實(shí)例詳解(獲取,存儲(chǔ),刪除等)
- jquery.cookie.js用法實(shí)例詳解
- jQuery基于cookie實(shí)現(xiàn)的購(gòu)物車實(shí)例分析
- jQuery操作cookie方法實(shí)例教程
- jQuery的cookie插件實(shí)現(xiàn)保存用戶登陸信息
- jquery.cookie() 方法的使用(讀取、寫入、刪除)
- jquery 刪除cookie失效的解決方法
- 可兼容IE的獲取及設(shè)置cookie的jquery.cookie函數(shù)方法
- 詳解jQuery的Cookie插件
相關(guān)文章
jQuery實(shí)現(xiàn) 上升、下降、刪除、添加一行代碼
這篇文章主要介紹了jQuery實(shí)現(xiàn) 上升、下降、刪除、添加一行代碼的實(shí)現(xiàn)方法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-03-03jQuery多個(gè)input求和的實(shí)現(xiàn)方法
這篇文章主要介紹了jQuery多個(gè)input求和的實(shí)現(xiàn)方法,涉及jQuery獲取input表單元素值及運(yùn)算的相關(guān)技巧,需要的朋友可以參考下2015-02-02jquery中插件實(shí)現(xiàn)自動(dòng)添加用戶的具體代碼
當(dāng)點(diǎn)擊添加時(shí)自動(dòng)彈出窗口,填寫完畢按確定自動(dòng)添加到table表格中,有需要的朋友可以參考一下2013-11-11用jquery實(shí)現(xiàn)點(diǎn)擊欄目背景色改變
用jquery如何實(shí)現(xiàn)點(diǎn)擊一欄目實(shí)現(xiàn)欄目變色,再點(diǎn)擊另一欄目也變色,但原來(lái)的變回原色,本文提供解決方法,需要了解的朋友可以參考下2012-12-12jQuery插件JWPlayer視頻播放器用法實(shí)例分析
這篇文章主要介紹了jQuery插件JWPlayer視頻播放器用法,結(jié)合實(shí)例形式分析了JWPlayer插件播放視頻的相關(guān)操作技巧,需要的朋友可以參考下2017-01-01jQuery插件FusionCharts繪制ScrollColumn2D圖效果示例【附demo源碼下載】
這篇文章主要介紹了jQuery插件FusionCharts繪制ScrollColumn2D圖效果,結(jié)合實(shí)例形式分析了jQuery使用FusionCharts插件結(jié)合swf文件實(shí)現(xiàn)ScrollColumn2D圖的繪制功能,并附帶demo源碼供讀者下載參考,需要的朋友可以參考下2017-03-03JQuery手速測(cè)試小游戲?qū)崿F(xiàn)思路詳解
這篇文章主要介紹了JQuery手速測(cè)試小游戲?qū)崿F(xiàn)思路詳解,本文給大家介紹的非常詳細(xì),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-09-09jQuery設(shè)置與獲取HTML,文本和值的簡(jiǎn)單實(shí)例
本篇文章主要是對(duì)jQuery設(shè)置與獲取HTML,文本和值的簡(jiǎn)單實(shí)例進(jìn)行了介紹,需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2014-02-02jQuery實(shí)現(xiàn)的簡(jiǎn)單懸浮層功能完整實(shí)例
這篇文章主要介紹了jQuery實(shí)現(xiàn)的簡(jiǎn)單懸浮層功能,結(jié)合完整實(shí)例形式分析了jQuery基于時(shí)間函數(shù)動(dòng)態(tài)修改css樣式實(shí)現(xiàn)窗口浮動(dòng)效果的相關(guān)技巧,需要的朋友可以參考下2017-01-01