jQuery ajax 问题
我已经研究这段代码一段时间了,但不确定我做错了什么。基本上我想创建一个循环来打印购物车中的所有商品。如果我取出循环,它会打印刚刚添加的项目,但是当我添加循环时,它会中断。
我对 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
请将您的呼叫更改为 $.each,如下所示:
Please change your call to $.each as follows:
以下应该有效。查看每个项是如何调用的,“this”指的是正在迭代的项目。我还更改了变量“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:
看来这一行是您的问题:
您正在将所有项目附加到最初附加到显示的对象。最好将所有后续项目直接附加到显示中,或者在填充项目对象后将其附加到显示中。
It looks like this line is your problem:
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.