试图将允许添加的商品总数限制为 4,此功能第一次发出警报,但仍将商品添加到购物车。
simpleCart.bind( 'beforeAdd' , function( item ){
if(simpleCart.quantity() === 4 ){
alert("You can only compare 4 items.");
return false;
}
});
试图将允许添加的商品总数限制为 4,此功能第一次发出警报,但仍将商品添加到购物车。
simpleCart.bind( 'beforeAdd' , function( item ){
if(simpleCart.quantity() === 4 ){
alert("You can only compare 4 items.");
return false;
}
});
它只检查添加的第 4 个元素并继续添加元素。如果您检查数量是否等于或大于 4 以防止添加更多项目:)
simpleCart.bind( 'beforeAdd' , function( item ){
if(simpleCart.quantity() >= 4 ){
alert("You can only compare 4 items.");
return false;
}
});
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;
}
});