2014-10-22 86 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; 
    } 
}); 
相關問題