jQuery ajax 问题

发布于 2024-08-06 01:42:34 字数 2145 浏览 3 评论 0原文

我已经研究这段代码一段时间了,但不确定我做错了什么。基本上我想创建一个循环来打印购物车中的所有商品。如果我取出循环,它会打印刚刚添加的项目,但是当我添加循环时,它会中断。

我对 jQuery/JSAON 不太熟悉,所以如果有人能指出我可能出错的地方,那就太好了。

(编辑-完整的js文件)

var ShoppCartAjaxHandler = function (cart) {
    (function($) {
        var display = $('#shopp-cart-ajax');
        display.empty().hide(); // clear any previous additions
        var item = $('<ul class="sidecart_list"></ul>').appendTo(display);
        $.each(cart.items, function() {
            $("<li></li>").html("<strong>"+this.quantity+"</strong>"+this.name).appendTo(item);
        });
        //$('<li></li>').html('<strong>'+cart.Item.quantity+'</strong> x '+cart.Item.name).appendTo(item);
        $('<li></li>').html('<strong>Subotal</strong> '+asMoney(cart.Totals.subtotal)).appendTo(item);
        $('<li></li>').html('<strong>Shipping</strong> '+asMoney(cart.Totals.shipping)).appendTo(item);
        $('<li></li>').html('<strong>Total</strong> '+asMoney(cart.Totals.total)).appendTo(item);
        if ($('#shopp-cart-items').length > 0) {
            $('#shopp-cart-items').html(cart.Totals.quantity);
            $('#shopp-cart-total').html(asMoney(cart.Totals.total));            
        } else {

            $('.widget_shoppcartwidget p.status').html('<p class="status_info"><strong><span id="shopp-cart-items">'+cart.Totals.quantity+'</span></strong> x <span id="shopp-cart-total">'+cart.Item.name+'</span></p><p class="status_info"><strong>Subtotal</strong> <span id="shopp-cart-subtotal">'+asMoney(cart.Totals.subtotal)+'</span></p><p class="status_info"><strong>Shipping</strong> <span id="shopp-cart-shipping">'+asMoney(cart.Totals.shipping)+'</span></p><p class="status_info"><strong>Total</strong> <span id="shopp-cart-total">'+asMoney(cart.Totals.total)+'</span></p>');

        }
        display.slideDown();
    })(jQuery)  
}

I've been at this bit of code for a while, but am not sure what I'm doing wrong. Basically I want to create a loop that prints out all items in a cart. If I take the loop out, it prints the item just added, but when I add the loop in, it breaks.

I'm not too familiar with jQuery/JSAON, so if anyone could point out where I might be going wrong, that would be great.

(edit - full js files)

var ShoppCartAjaxHandler = function (cart) {
    (function($) {
        var display = $('#shopp-cart-ajax');
        display.empty().hide(); // clear any previous additions
        var item = $('<ul class="sidecart_list"></ul>').appendTo(display);
        $.each(cart.items, function() {
            $("<li></li>").html("<strong>"+this.quantity+"</strong>"+this.name).appendTo(item);
        });
        //$('<li></li>').html('<strong>'+cart.Item.quantity+'</strong> x '+cart.Item.name).appendTo(item);
        $('<li></li>').html('<strong>Subotal</strong> '+asMoney(cart.Totals.subtotal)).appendTo(item);
        $('<li></li>').html('<strong>Shipping</strong> '+asMoney(cart.Totals.shipping)).appendTo(item);
        $('<li></li>').html('<strong>Total</strong> '+asMoney(cart.Totals.total)).appendTo(item);
        if ($('#shopp-cart-items').length > 0) {
            $('#shopp-cart-items').html(cart.Totals.quantity);
            $('#shopp-cart-total').html(asMoney(cart.Totals.total));            
        } else {

            $('.widget_shoppcartwidget p.status').html('<p class="status_info"><strong><span id="shopp-cart-items">'+cart.Totals.quantity+'</span></strong> x <span id="shopp-cart-total">'+cart.Item.name+'</span></p><p class="status_info"><strong>Subtotal</strong> <span id="shopp-cart-subtotal">'+asMoney(cart.Totals.subtotal)+'</span></p><p class="status_info"><strong>Shipping</strong> <span id="shopp-cart-shipping">'+asMoney(cart.Totals.shipping)+'</span></p><p class="status_info"><strong>Total</strong> <span id="shopp-cart-total">'+asMoney(cart.Totals.total)+'</span></p>');

        }
        display.slideDown();
    })(jQuery)  
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

合约呢 2024-08-13 01:42:35

请将您的呼叫更改为 $.each,如下所示:

$.each(
  cart.items,
  function() {
    $("<li></li>").html("<strong>" + this.Quantity + "</strong>" +
      this.name).appendTo(item);
  }
);

Please change your call to $.each as follows:

$.each(
  cart.items,
  function() {
    $("<li></li>").html("<strong>" + this.Quantity + "</strong>" +
      this.name).appendTo(item);
  }
);
猛虎独行 2024-08-13 01:42:35

以下应该有效。查看每个项是如何调用的,“this”指的是正在迭代的项目。我还更改了变量“ul”来表示列表,以便更清楚发生了什么。最后,确保您的购物车如下所示:

var cart = { 
    items: [
        {quantity: 1, name: 'asjdfkj' },
        {quantity: 1, name: 'asjdfkj' },
        {quantity: 1, name: 'bkag' }
            ],
    Totals: { 
        subtotal: 12.95, 
        shipping: 2.34, 
        total: 15.00
    }

};

var asMoney=function(s) { return s; }


    var display = $('#shopp-cart-ajax');
    display.empty(); // clear any previous additions
    var ul = $('<ul class="sidecart_list"></ul>').appendTo(display);

    $.each(cart.items, function() {
      $('<li></li>').html('<strong>'+this.quantity+'</strong> x '+this.name).appendTo(ul);
    });
 $('<li></li>').html('<strong>Subotal</strong>'+asMoney(cart.Totals.subtotal)).appendTo(ul);
 $('<li></li>').html('<strong>Shipping</strong>'+asMoney(cart.Totals.shipping)).appendTo(ul);
    $('<li></li>').html('<strong>Total</strong> '+asMoney(cart.Totals.total)).appendTo(ul);

The following should work. Check out how each is called, and "this" refers to the items being iterated on. I also changed the variable "ul" to represent the list, so that it is clearer what is going on. Finally, make sure your cart looks like this:

var cart = { 
    items: [
        {quantity: 1, name: 'asjdfkj' },
        {quantity: 1, name: 'asjdfkj' },
        {quantity: 1, name: 'bkag' }
            ],
    Totals: { 
        subtotal: 12.95, 
        shipping: 2.34, 
        total: 15.00
    }

};

var asMoney=function(s) { return s; }


    var display = $('#shopp-cart-ajax');
    display.empty(); // clear any previous additions
    var ul = $('<ul class="sidecart_list"></ul>').appendTo(display);

    $.each(cart.items, function() {
      $('<li></li>').html('<strong>'+this.quantity+'</strong> x '+this.name).appendTo(ul);
    });
 $('<li></li>').html('<strong>Subotal</strong>'+asMoney(cart.Totals.subtotal)).appendTo(ul);
 $('<li></li>').html('<strong>Shipping</strong>'+asMoney(cart.Totals.shipping)).appendTo(ul);
    $('<li></li>').html('<strong>Total</strong> '+asMoney(cart.Totals.total)).appendTo(ul);
很糊涂小朋友 2024-08-13 01:42:35

看来这一行是您的问题:

var item = $('<ul class="sidecart_list"></ul>').appendTo(display);

您正在将所有项目附加到最初附加到显示的对象。最好将所有后续项目直接附加到显示中,或者在填充项目对象后将其附加到显示中。

It looks like this line is your problem:

var item = $('<ul class="sidecart_list"></ul>').appendTo(display);

You are appending all your items to an object that was only initially appended to your display. It might be best to just append all following items straight to the display or append the item object to the display after it's been populated.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文