我开发了一个代码,其中添加值,最后根据您在表单中选择的项目减去最小值。代码工作正常,但是当您取消选择所有项目而不是显示 0 时会出现问题,而是显示 -Infinity。我该如何处理这个脚本来强制它显示 0 而不是 -Infinity?
// All selected prices are stored on a array
var prices = [];
// A function to remove a item from an array
function remove(array, item) {
for (var i = 0, len = array.length; i < len; i++) {
if (array[i] == item) {
array.splice(i, 1);
return true;
}
}
return false;
}
function calculateSectedDues(checkbox, amount) {
// We add or remove the price as necesary
if (checkbox.checked) {
prices.push(amount);
} else {
remove(prices, amount);
}
// We sum all the prices
var total = 0;
for (var i = 0, len = prices.length; i < len; i++)
total += prices[i];
// We get the lower one
var min = Math.min.apply(Math, prices);
// And substract it
total -= min;
// Please, don't access the DOM elements this way, use document.getElementById instead
document.grad_enroll_form.total.value = total;
}
</script>