[英]Setting a maximum quantity value with Javascript
I have this javascript to prevent quantity fields going below 1. What I would like to do is make sure the maximum quantity is 10 and can't go below 1.我有这个 javascript 来防止数量字段低于 1。我想做的是确保最大数量是 10,并且不能 go 低于 1。
/**
* When using the quantity selector, this can be used to decrease the quantity (be ensuring it won't be lower than 1)
*/
}, {
key: '_decreaseQuantity',
value: function _decreaseQuantity(event, target) {
target.nextElementSibling.value = Math.max(parseInt(target.nextElementSibling.value) - 1, 1);
}
/**
* When using the quantity selector, this can be used to increase the quantity
*/
}, {
key: '_increaseQuantity',
value: function _increaseQuantity(event, target) {
target.previousElementSibling.value = parseInt(target.previousElementSibling.value) + 1;
}
/**
* Make sure the quantity does not go below when manually changed
*/
}, {
key: '_validateQuantity',
value: function _validateQuantity(event, target) {
target.value = Math.max(parseInt(target.value) || 1, 1);
}
}]);
Replicating what you already did to keep the value from going below 1 is enough.复制你已经做的以防止值低于 1 就足够了。
{
key: '_decreaseQuantity',
value: function _decreaseQuantity(event, target) {
target.nextElementSibling.value = Math.max(parseInt(target.nextElementSibling.value) - 1, 1);
}
/**
* When using the quantity selector, this can be used to increase the quantity
*/
}, {
key: '_increaseQuantity',
value: function _increaseQuantity(event, target) {
target.previousElementSibling.value = Math.min(parseInt(target.previousElementSibling.value) + 1, 10); // Added check
}
/**
* Make sure the quantity does not go below when manually changed
*/
}, {
key: '_validateQuantity',
value: function _validateQuantity(event, target) {
target.value = Math.min(Math.max(parseInt(target.value) || 1, 1), 10); // Added check
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.