javascript - 无法修复购物车中的总计功能

标签 javascript jquery ajax cart shopping

我是购物人员示例中的编解码和本教程的新手,它是:
http://jumpstartlab.com/resources/jquery-jumpstart/jscart---a-jquery-shopping-cart/

当我尝试将购物车中的数量总计时遇到麻烦了。总计是一些奇怪的数字,例如库存数量的倍数。如果有人有任何想法可以帮助我解决错误,我将非常感激。这是有问题的部分:

var JSCart = {
  update_cart_item_count : function () {
    var items = $('#cart div.cart_item');
    var total = 0;

    items.each(function (){

        var quant = items.find('span.qty');
        var value = parseInt(quant.text());        
        total = total + value;
        $('span#cart_quantity').text(total);
        });
},


这就是整个过程,包括以下部分:

$(document).ready(function (){

var inventory = $(raw_inventory); 

var prototype_item = $('#prototype_item');
prototype_item.detach();    

var prototype_cart = $('#prototype_cart');
prototype_cart.detach();  

var JSCart = {
    update_cart_item_count : function () {
        var items = $('#cart div.cart_item');
        var total = 0;

        items.each(function (){

            var quant = items.find('span.qty');
            var value = parseInt(quant.text());        
            total = total + value;
            $('span#cart_quantity').text(total);
            });
    },
    update_cart_total : function () {   
    },
    update_cart : function () {
         this.update_cart_item_count();
         this.update_cart_total();
         //alert('Updating the cart...');
         }
    };

 inventory.each(function(){
  //     alert("Inserting " + this.name);
       var item = prototype_item.clone();  
            item.find('h3').text(this.name);
            item.find('span.price').text(this.price);
            item.find('span.qty').text(this.stock);
            $('div#prototype_item').attr('id', 'product_' + this.product_id);
            $('#inventory').append(item);

       var cart_item = prototype_cart.clone();
            cart_item.find('h3').text(this.name);              
            $('div#prototype_cart').attr('id', 'product_' + this.product_id);
            $('#cart').append(cart_item);

       item.click(function () {
        //alert("Adding " + $(this).attr('id') + " to the cart." );
            var target_id = $(this).attr('id'); 
            var target = $('div#cart div#' + target_id); 

            var quantity = target.find('span.qty'); 
            var current = parseInt(quantity.text()); 
            $(quantity).text(current + 1); 
            JSCart.update_cart();
        });

  });
});


非常感谢!

最佳答案

我相信您的问题在于:

var quant = items.find('span.qty');


因为您要遍历项目,但在调用items.find时未为迭代中的每个项目提供值,所以它会找到所有的qty值,这意味着您在解析时总是得到第一个的文本。它是一个整数。

您可能想要这样的东西:

$('div.cart_item').each(function (i, item) {})

关于javascript - 无法修复购物车中的总计功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8365392/

相关文章:

jQuery datepicker 插件在 Chrome 和 Safari 中不起作用

javascript - 处理 Javascript 中的异步调用

java - 使用 URLConnection 访问时连接超时

Javascript JSON 数据操作库

javascript - 如何增加十六进制数

javascript - 单击时禁用其他输入

ruby-on-rails - "Nil location provided. Can' t build URI."在 rails 中执行 AJAX 请求时是什么意思?

javascript - 将鼠标悬停在另一个元素上时显示一个元素

javascript - JSTree:移动所有子节点

jquery - 删除动态 JQuery UI 选项卡