码迷,mamicode.com
首页 > Web开发 > 详细

shoppingCart.js

时间:2014-07-18 14:34:38      阅读:254      评论:0      收藏:0      [点我收藏+]

标签:返回顶部   style   blog   http   java   color   

ylbtech-JavaScript-util: shoppingCart.js

 购物车脚本

1.A,JS-效果图返回顶部
 
1.B,JS-Source Code(源代码)返回顶部
1.B.1,m.yintai
bubuko.com,布布扣
/*
*/
function Product() {
    var itemcode; //商品编码
    var count; //数量
    var type; //类型
    var paramid; //参数
}
/*
添加商品
*/
function addShoppingCartProduct(itemCode, count, type, paramid) {
    if (itemCode == "" || itemCode == null) {
        alert("商品编号不能为空!");
        return false;
    }
    var productList = getShoppingCartProductList();
    var found = false;
    var i = 0;
    while (i < productList.length) {
        var productInfo = productList[i];
        if (productInfo.itemcode == itemCode) {
            productInfo.count = productInfo.count + count;
            productInfo.type = type;
            productInfo.paramid = paramid;
            found = true;
            break;
        }
        i++;
    }
    if (!found) {
        product = new Object();
        product.itemcode = itemCode;
        product.count = count;
        product.type = type;
        product.paramid = paramid;
        productList.push(product);
    }
    localStorage.shoppingCartProductList = JSON.stringify(productList);
    return true;
}
/*
取得商品
*/
function getShoppingCartProduct(itemCode) {
    var productList = getShoppingCartProductList();
    var i = 0;
    while (i < productList.length) {
        var productInfo = productList[i];
        if (productInfo.itemcode == itemCode) {
            return productInfo;
        }
        i++;
    }
    return null;
}
/*
取得商品列表
*/
function getShoppingCartProductList() {
    if (localStorage.shoppingCartProductList == undefined || localStorage.shoppingCartProductList == "undefined" || localStorage.shoppingCartProductList == "") {
        var newProductList = new Array();
        localStorage.shoppingCartProductList = JSON.stringify(newProductList);
    }
    var productList = JSON.parse(localStorage.shoppingCartProductList);
    return productList;
}
/*
清空购物车
*/
function clearShoppingCart() {
    var productList = new Array();
    localStorage.shoppingCartProductList = JSON.stringify(productList);
}
/*
删除商品
*/
function removeShoppingCartProduct(itemCode) {
    var productList = getShoppingCartProductList();
    var i = 0;
    while (i < productList.length) {
        var productInfo = productList[i];
        if (productInfo.itemcode == itemCode) {
            productList.splice(i, 1);
            break;
        }
        i++;
    }
    localStorage.shoppingCartProductList = JSON.stringify(productList);
}
/*
修改商品数量
*/
function updateShoppingCartProduct(itemCode, count) {
    var productList = getShoppingCartProductList();
    var i = 0;
    while (i < productList.length) {
        var productInfo = productList[i];
        if (productInfo.itemcode == itemCode) {
            productInfo.count = count;
            break;
        }
        i++;
    }
    localStorage.shoppingCartProductList = JSON.stringify(productList);
}
View Code

1.B.2,

1.C,JS-Relevent References(相关引用)返回顶部

 

bubuko.com,布布扣 作者:ylbtech
出处:http://ylbtech.cnblogs.com/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

shoppingCart.js,布布扣,bubuko.com

shoppingCart.js

标签:返回顶部   style   blog   http   java   color   

原文地址:http://www.cnblogs.com/ylbtech/p/3852595.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!