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)  
}
4

3 に答える 3

1

次のように呼び出しを $.each に変更してください。

$.each(
  cart.items,
  function() {
    $("<li></li>").html("<strong>" + this.Quantity + "</strong>" +
      this.name).appendTo(item);
  }
);
于 2009-09-26T02:12:49.020 に答える
1

以下はうまくいくはずです。それぞれがどのように呼び出されるかを確認してください。「これ」は、反復されるアイテムを指します。また、変数「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);
于 2009-09-26T02:46:06.103 に答える
0

この行があなたの問題のようです:

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

ディスプレイに最初に追加されただけのオブジェクトにすべてのアイテムを追加しています。次のすべてのアイテムをディスプレイに直接追加するか、アイテムが入力された後にアイテムオブジェクトをディスプレイに追加するのが最適な場合があります。

于 2009-09-27T09:40:26.153 に答える