2012-09-18 75 views
0

我对JavaScript有疑问。我有一个购物车和一个从购物车中删除产品的功能。购物车为空时,如何重定向到主页面?如何从购物车中删除商品

这是我的功能删除产品。

function removeCart(key) { 
$.ajax({ 
    url: 'index.php?route=checkout/cart/update', 
    type: 'post', 
    data: 'remove=' + key, 
    dataType: 'json', 
    success: function(json) { 
     $('.success, .warning, .attention, .information').remove(); 

     if (json['output']) { 
      $('#cart_total').html(json['total']); 
      $("table.total tr:last td:last").text(json['total'].split('-')[1]); 

      $('#cart .content').html(json['output']); 
     }   
    } 
}); 
} 
+1

location.href =“/主页” – FLCL

+0

我知道,但是当我必须这样做吗?当购物车是空的时我怎么知道这个 – Stars

+0

@Stars只是检查是否没有产品。 –

回答

1

您应该在进行更新时购买您的购物车。如果购物车是空的,你可以在你的ajax响应中返回。例如,通过您的数组中设置一个emptyCart键:

function removeCart(key) { 
$.ajax({ 
    url: 'index.php?route=checkout/cart/update', 
    type: 'post', 
    data: 'remove=' + key, 
    dataType: 'json', 
    success: function(json) { 
     $('.success, .warning, .attention, .information').remove(); 
     if (json['emptyCart']) { 
      location.href="/where-you-want-it-to-go"; 
     } 
     if (json['output']) { 
      $('#cart_total').html(json['total']); 
      $("table.total tr:last td:last").text(json['total'].split('-')[1]); 

      $('#cart .content').html(json['output']); 
     } 
    } 
}); 
} 
+0

Thx,您的示例对我有帮助 – Stars

+0

很好听:) –

相关问题