• 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;
        };
    }
  • 相关阅读:
    基于jquery的提示框JavaScript 插件,类Bootstrap
    css重点知识和bug解决方法
    小白初识js
    动态的将数据生成“表格”(京东商品),并具备搜索筛选工能
    原装js轮播图,鼠标移入停止轮播,移出继续轮播
    用js写九九乘法表格,附带背景颜色
    用js写直角三角形,等腰三角形,菱形
    关于两数组,求出两数组中相同的的对象
    好的js书写习惯
    前端之道
  • 原文地址:https://www.cnblogs.com/fuyu-blog/p/4736256.html
Copyright © 2020-2023  润新知