js+cookie 购物车
$(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 购物车的更多相关文章
- 原生JS实现购物车结算功能代码+zepto版
html <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3 ...
- js COOKIE 记住帐号或者uuid
当开始接到这个任务的时候,我对cookie还是没多少了解的,而uuid的生成也是一无所知.但是当你发现这个网址http://stackoverflow.com/questions/105034/how ...
- [JS]Cookie精通之路
[JS]Cookie精通之路 转http://blog.163.com/neu_pdh1983/blog/static/572407020077310528915/ 发布:Cary 媒体:www.Ju ...
- JS Cookie丢失问题
JS Cookie丢失问题 前些天有人问我vue中使用proxy发送请求,为什么请求时cookie丢失,首先说一下我对cookie的理解: 1.cookie在正常情况下是会在每次请求时自动携带, 2. ...
- JS实现购物车02
需求使用JS实现购物车功能02 具体代码 <!DOCTYPE html> <html lang="en"> <head> <meta ch ...
- JS实现购物车01
需求 使用JS实现购物车功能01 具体代码 <!DOCTYPE html> <html lang="en"> <head> <meta c ...
- vue.js实现购物车功能
购物车是电商必备的功能,可以让用户一次性购买多个商品,常见的购物车实现方式有如下几种: 1. 用户更新购物车里的商品后,页面自动刷新. 2. 使用局部刷新功能,服务器端返回整个购物车的页面html 3 ...
- 用vue.js实现购物车功能
购物车是电商必备的功能,可以让用户一次性购买多个商品,常见的购物车实现方式有如下几种: 1. 用户更新购物车里的商品后,页面自动刷新. 2. 使用局部刷新功能,服务器端返回整个购物车的页面html 3 ...
- js cookie创建读取删除函数封装
js cookie创建读取删除函数封装 一.总结 都是为了方便操作,这样弄了很方便 1.创建cookie的函数封装的作用:方便设置过期时间expire,方便设置键和值 2.查询cookie的数据封装的 ...
随机推荐
- Linux常见疑难问答
Linux常见疑难问答 (1)按a~z顺序排列启动服务进程. #exportLC_ALL=C #英文环境变量设置,主要用于解决乱码问题 #chkconfig –list | gre ...
- 调用百度地图API出现 error inflating class com.baidu.mapapi.map.mapview
问题如下 本来以为解决了,但后来重新运行了一下,又坏了,然后改成原来的样子,又好了. 结果就是:对不住了各位看官,没找到解决办法,不过经测试有个地方,可能在程序运行时,出现error inflatin ...
- ARM仿真器
ARM仿真器需要将调试换成simulator,其他JLink设置不变.否则会提示无法装载.flash文件 在keil下开发的设置
- 你注意了么?int与Integer的区别
上次做项目时遇到了一个小问题. 我把javabean中的custid属性定义为int类型,当然与数据库中相应类型是一致的,而且在hibernate文件中配置时专门设置了not-null="f ...
- lambda匿名函数
1.python中的匿名函数的格式 lambda arg1,arg2...,argN:expression (lambda关键字后,冒号":"前是参数,多个参数用逗号&qu ...
- Programming Entity Framework 翻译(1)-目录
1. Introducing the ADO.NET Entity Framework ado.net entity framework 介绍 1 The Entity Relationship Mo ...
- java ppt课后作业
1 .仔细阅读示例: EnumTest.java,运行它,分析运行结果? 枚举类型是引用类型,枚举不属于原始数据类型,它的每个具体值都引用一个特定的对象.相同的值则引用同一个对象.可以使用“==”和e ...
- C# iis 错误配置信息( 500.19 - Internal Server Error )
出现这个错误是因为 IIS 7 采用了更安全的 web.config 管理机制,默认情况下会锁住配置项不允许更改. 要取消锁定可以以管理员身份运行命令行 %windir%\system3 ...
- MySQL连接线程kill利器之pt-kill
如何每10秒检查一次,杀死指定用户超过100秒的查询? pt-kill \ --no-version-check \ --host 127.0.0.1 --port 3306 --user 'xxxx ...
- 使用openssl实现ECDSA签名以及验证功能(附完整测试源码)
突然找到数年前写的这段代码,当是因为对密码学几乎不怎么了解踩了一些坑,现在开源出来方便大家直接利用. ECDSA的全名是Elliptic Curve DSA,也就是椭圆曲线DSA,由于椭圆曲线的复杂性 ...