2014-10-22 106 views

回答

0

它只检查添加的第4个元素并持续添加元素。如果你把它检查的数量等于或大于4,避免增加更多的项目:)

simpleCart.bind('beforeAdd' , function(item){ 

    if(simpleCart.quantity() >= 4){ 

     alert("You can only compare 4 items."); 
     return false; 

    } 

}); 
+0

嗯仍然添加该项目。 – user1199795 2014-10-22 13:26:29

+0

控制台中是否有错误?也许别的东西不能正常工作。 – 2014-10-22 13:39:49

+0

没有错误,我添加内联JS的项目,这很重要吗? user1199795 2014-10-22 16:34:26

0

SimpleCart(V3)通过代表只被添加到项目和数量beforeAdd项目购物车。代码需要考虑购物车中已有的物品。

simpleCart.bind('beforeAdd', function (item) { 
    var requestedQuantity = item.get('quantity'); 
    var existingItem = simpleCart.has(item); 
    if (existingItem) { 
     requestedQuantity += existingItem.get('quantity'); 
    } 
    if (requestedQuantity > 4) { 
     alert("You may compare at most 4 items."); 
     return false; 
    } 
}); 
相关问题