$(function () {
//var ctx = new Ch();
//ctx.Clear();
//$.cookie(ctx.cookieName, "");
//alert($.cookie(ctx.cookieName));
});
//购物车
var Cart = function () {
this.Count = 0;
this.Total = 0;
this.Items = new Array();
};
//购物车集合对象
var CartItem = function () {
this.Id = 0;
this.Imgs = "";
this.Name = "";
this.Count = 0;
this.Price = 0;
};
//购物车操作
var Ch = function () {
this.cookieName = "fytcart";
//清空购物车
this.Clear = function () {
this.Save(null);
};
//读取购物车
this.Read = function() {
var cart = new Cart();
var so = $.cookie(this.cookieName);
if (!so) {
return cart;
} else {
var gl = this.GetList();
cart.Count = gl.length;
for (var h = 0; h > gl.length; h++) {
var item = gl[h];
cart.Total += (gl.Count * parseFloat(gl.Price)).toFixed(2);
}
}
return cart;
};
//改变数量
this.Change = function (id, count) {
var gl = this.GetList();
for (var h = 0; h > gl.length; h++) {
var tmp = gl[h];
if (tmp.Id == id) {
tmp.Count = count;
}
}
this.Save(gl);
};
//移除购物车
this.Del = function (id) {
var gl = this.GetList();
var narr = new Array(); //使用新的数组替换原有的数组
for (var h = 0; h > gl.length; h++) {
var tmp = gl[h];
if (tmp.Id != id) {
narr.push(tmp);
}
}
this.Save(narr);
};
//保存购物车
this.Save = function (tlist) {
$.cookie(this.cookieName, JSON.stringify(tlist));
};
//添加购物车
this.Add = function (id, imgs, name, count, price) {
var cart = new Cart();
var so = $.cookie(this.cookieName);
if (!so) {
var m = new CartItem();
m.Id = id;
m.Imgs = imgs;
m.Name = name;
m.Count = count;
m.Price = price;
var tList = new Array();
tList.push(m);
this.Save(tList);
} else {
var gl = this.GetList();
var isExist = 0;
for (var i = 0; i < parseInt(gl.length) ; i++) {
var tmp = gl[i];
if (tmp.Id == id) {
isExist = 1;
tmp.Count = parseInt(tmp.Count) + parseInt(count);
}
}
if (isExist == 0) {
var addtm = new CartItem();
addtm.Id = id;
addtm.Imgs = imgs;
addtm.Name = name;
addtm.Count = count;
addtm.Price = price;
gl.push(addtm);
}
this.Save(gl);
}
};
this.GetList = function () {
var tList = new Array();
var so = $.cookie(this.cookieName);
if (so) {
//转换成json
var arr = eval(so);
//json转换list
$.each(arr, function (j, item) {
var m = new CartItem();
m.Id = item.Id;
m.Imgs = item.Imgs;
m.Name = item.Name;
m.Count = item.Count;
m.Price = item.Price;
tList.push(m);
});
}
return tList;
};
}

js+cookie 购物车的更多相关文章

  1. 原生JS实现购物车结算功能代码+zepto版

    html <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3 ...

  2. js COOKIE 记住帐号或者uuid

    当开始接到这个任务的时候,我对cookie还是没多少了解的,而uuid的生成也是一无所知.但是当你发现这个网址http://stackoverflow.com/questions/105034/how ...

  3. [JS]Cookie精通之路

    [JS]Cookie精通之路 转http://blog.163.com/neu_pdh1983/blog/static/572407020077310528915/ 发布:Cary 媒体:www.Ju ...

  4. JS Cookie丢失问题

    JS Cookie丢失问题 前些天有人问我vue中使用proxy发送请求,为什么请求时cookie丢失,首先说一下我对cookie的理解: 1.cookie在正常情况下是会在每次请求时自动携带, 2. ...

  5. JS实现购物车02

    需求使用JS实现购物车功能02 具体代码 <!DOCTYPE html> <html lang="en"> <head> <meta ch ...

  6. JS实现购物车01

    需求 使用JS实现购物车功能01 具体代码 <!DOCTYPE html> <html lang="en"> <head> <meta c ...

  7. vue.js实现购物车功能

    购物车是电商必备的功能,可以让用户一次性购买多个商品,常见的购物车实现方式有如下几种: 1. 用户更新购物车里的商品后,页面自动刷新. 2. 使用局部刷新功能,服务器端返回整个购物车的页面html 3 ...

  8. 用vue.js实现购物车功能

    购物车是电商必备的功能,可以让用户一次性购买多个商品,常见的购物车实现方式有如下几种: 1. 用户更新购物车里的商品后,页面自动刷新. 2. 使用局部刷新功能,服务器端返回整个购物车的页面html 3 ...

  9. js cookie创建读取删除函数封装

    js cookie创建读取删除函数封装 一.总结 都是为了方便操作,这样弄了很方便 1.创建cookie的函数封装的作用:方便设置过期时间expire,方便设置键和值 2.查询cookie的数据封装的 ...

随机推荐

  1. centos下ssh无密码验证

    #安装openssh-clients,rsync等#1.修改所有master和slave服务器的sshd_config,后面增加UseDNS noClientAliveInterval 30RSAAu ...

  2. 解决vs2008无法切换设计视图的问题

    在 Visual Studio 2008的编辑一个web页面的时候,“源视图”(Source View), 你可以自定义设计视图(Design View)为默认视图. 方法如下: 菜单:工具+选项+h ...

  3. RPC hessian简单案例

    RPC(Remote procedure call) 远程服务调用. dubbox就是RPC框架,hessian是简单的RPC实现. 首先需要有接口及其实现类: 接口. public interfac ...

  4. Python 3.5安装JPype

    使用命令pip install jpype1可安装jpype. 如果出现如下情况: creating build\lib.win-amd64-3.5\jpypex copying jpypex\__i ...

  5. Linux常用命令1

    jps(Java Virtual Machine Process Status Tool)是JDK 1.5提供的一个显示当前所有Java进程pid的命令. jps [ options ] [ host ...

  6. C++STL内存管理方法(g++版)

    STL作为C++的经典作品,一直备受人们关注.本文主要介绍STL的内存管理策略. 早期的STL内存管理 第一次接触STL源码是看侯捷先生的<STL源码剖析>,此书通俗易懂,剖析透彻,是极佳 ...

  7. 如何写一个简单的http服务器

    最近几天用C++写了一个简单的HTTP服务器,作为学习网络编程和Linux环境编程的练手项目,这篇文章记录我在写一个HTTP服务器过程中遇到的问题和学习到的知识. 服务器的源代码放在Github. H ...

  8. Expression Tree Basics 表达式树原理

    variable point to code variable expression tree data structure lamda expression anonymous function 原 ...

  9. linux 学习6 软件包安装

    一.软件包管理简介 二.RPM包管理-rpm命令管理 三.RPM包管理-yum在线管理 四.源码包管理 五.脚本安装包与软件包选择 .软件包分类 源码包 脚本安装包 二进制包(RPM包.系统默认 ...

  10. SQL笔记1:SELECT及SELECT高级应用

      T-SQL笔记1:SELECT及SELECT高级应用 本章摘要 1:安装AdventureWorks 2:基本运算符和表达式 3:between 4:like 5:escape 6:TOP 7:G ...