jquery.cookie实现的客户端购物车操作实例_jquery_脚本之家

本文实例讲述了jquery.cookie实现的客户端购物车操作。分享给大家供大家参考,具体如下:

//购物车var Cart = function () { this.Count = 0; this.Total = 0; this.Items = new Array();};//购物车集合对象var CartItem = function () { this.Id = 0; this.Name = ""; this.Count = 0; this.Price = 0;}; //购物车操作var CartHelper = function () { this.cookieName = "yxhCart"; this.Clear = function () { var cart = new Cart; return cart; }; //向购物车添加 this.Add = function (id, name, count, price) { var cart = this.Read(); var index = this.Find; //如果ID已存在,覆盖数量 if  { 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; cart.Count++; cart.Total += (((cart.Items[index].Count * 100) * (cart.Items[index].Price * 100)) / 10000); } this.Save; return cart; }; //改变数量 this.Change = function  { var cart = this.Read(); var index = this.Find; cart.Items[index].Count = count; this.Save; return cart; }; //移出购物车 this.Del = function  { var cart = this.Read(); var index = this.Find { var item = cart.Items[index]; cart.Count--; cart.Total = cart.Total -  *  / 10000); cart.Items.splice; this.Save; } return cart; }; //根据ID查找 this.Find = function  { 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  { var source = ""; for (var i = 0; i < cart.Items.length; i++) { if  { source += "|$|"; } source += this.ItemToString; } $.cookie(this.cookieName, source); }; this.Read = function () { //读取COOKIE中的集合 var source = $.cookie; 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; cart.Items.push; cart.Total +=  *  / 10000); } return cart; }; this.ItemToString = function  { return item.Id + "||" + escape + "||" + item.Count + "||" + item.Price; }; this.ItemToObject = function  { var arr = str.split; var item = new CartItem(); item.Id = arr[0]; item.Name = unescape; item.Count = arr[2]; item.Price = arr[3]; return item; };};

希望本文所述对大家jQuery程序设计有所帮助。

相关文章

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图