performance - How to update Shopping Cart Total properly in Javascript? - Stack Overflow

I am looking for some Javascript that will:Update Cart Total on products selected (drop-down menu) and

I am looking for some Javascript that will:

  • Update Cart Total on products selected (drop-down menu) and quantities entered

(*Security will be handled by back-end PHP. I have attempted this over the last three days but apparently my code was so bad I feel ashamed to repost it here again)


My Thinking:
- Create a cart object
- Make a function that recalculates the total on any changes that occur
(I can not think of anything else this would require given the javascript will just be passing this over to a PHP script to check anyway)

Does anyone know of some javascript that does the job I seek? Is my thinking correct?

I am looking for some Javascript that will:

  • Update Cart Total on products selected (drop-down menu) and quantities entered

(*Security will be handled by back-end PHP. I have attempted this over the last three days but apparently my code was so bad I feel ashamed to repost it here again)


My Thinking:
- Create a cart object
- Make a function that recalculates the total on any changes that occur
(I can not think of anything else this would require given the javascript will just be passing this over to a PHP script to check anyway)

Does anyone know of some javascript that does the job I seek? Is my thinking correct?

Share Improve this question edited Apr 9, 2013 at 22:27 user1228 asked Mar 4, 2013 at 15:35 William JamesWilliam James 1331 gold badge1 silver badge10 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 3

Below is sample shopping cart javascript object built using revealing module pattern.

var shoppingCart = function () {
        var obj = {}, items = [];

        obj.AddItem = function (itemNo, quantity, price) {
            var item = {};
            item.itemNo = itemNo;
            item.quantity = quantity;
            item.price = price;
            items.push(item)
        };

        obj.GetAllItems = function () {
            return items;
        };

        obj.GetItemByNo = function (item) {
            for (var i = 0; i < items.length; i++) {
                if (items[i].itemNo === item)
                    return item[i];
            }
            return null;
        };

        obj.CalculateTotal = function () {
            var total = 0;
            for (var i = 0; i < items.length; i++) {
                total = total + (items[i].quantity * items[i].price);
            }
            return total;
        };

        return obj;
    };
    var cart = new shoppingCart();

Add items using AddItem method, include additional properties that are useful in the UI.

shoppingcart.AddItem(2,4,2.4);

Gets list of items in the shopping cart

var items =  shoppingcart.GetAllItems();

Calculates total price of items in shopping cart

var total = shoppingcart.CalculateTotal();

Please note that I have typed this code in here, so might contain typo's and also I remend having data type validations for price and quantity as numerics before adding item.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744854152a4597306.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信