[英]How do you call a specific value from inside a math function?
在此代碼中,我基於表單添加了多個值,並減去了最小值,從而創建了一個新的總計。 該代碼很好用。 我想做的是,而不是只在底部有一個新的總數,而是3。一個表示原始總數而不減去的值,一個具有我減去的最低價格的值,最后是實際值的值。新總數。 截至目前,它僅顯示新的總數。
示例:有3件圍巾(5美元),帽子(10美元)和夾克(20美元)。 我將所有等於35美元的值相加,然后減去最低值5美元的圍巾,得到新的總計30美元。 我想在底部顯示它。
總計:$ 35折扣:$ -5新總計:$ 30
該函數已經計算出所有這些值,我只是不知道如何從函數中調用它們。
// 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);
if(min == Infinity) { min = 0; };
// And substract it
total -= min;
document.grad_enroll_form.total.value = total;
}
這是一些基本代碼,它們只寫出值。 我仍然不同意您從數組中刪除元素的循環。
<span id="value"></span>
var prices = [];
function remove(arr,itm){
var indx = arr.indexOf(itm);
if (indx !== -1){
arr.splice(indx,1);
}
}
function calculateSectedDues(checkbox, amount) {
if (checkbox.checked === true) {
prices.push(amount);
} else {
remove(prices, amount);
}
var total = 0;
for (var i = 0, len = prices.length; i < len; i++)
total += prices[i];
var min = prices.slice().sort(function(a,b){return a-b})[0];
if(typeof min === 'undefined') min = 0;
var withDiscount = total - min;
var discountAmount = withDiscount - total;
document.querySelector("#value").innerHTML = "Total: $"+total+'<br>';
document.querySelector("#value").innerHTML += "Discount: $"+discountAmount+'<br>';
document.querySelector("#value").innerHTML += "New total: $"+withDiscount+'<br>';
}
像這樣運行:
calculateSectedDues({checked:true},10);
calculateSectedDues({checked:true},20);
calculateSectedDues({checked:true},5);
給出以下輸出:
總計:$ 35
折扣:$-5
總計:$ 30
<label><input type="checkbox" onclick="calculateSectedDues(this,5)" name="Scarf"> <span>Scarf</span><label><br>
<label><input type="checkbox" onclick="calculateSectedDues(this,10)" name="Hat"> <span>Hat</span><label><br>
<label><input type="checkbox" onclick="calculateSectedDues(this,20)" name="Jacket"> <span>Jacket</span><label><br>
<span id="value">Total: $0<br>Discount: $0<br>New total: $0</span>
我不知道您的HTML是什么樣的,但是類似的東西會起作用。 它使用jQuery捕獲所有已選中的復選框,並將這些復選框的值添加到總計中,並跟蹤最小值。 然后,它在單個對象中返回所有三個值。
我假設您的復選框的值是商品的價格,可能不正確。 如果沒有,這個想法仍然有效,您只需要做更多的工作就能獲得正確的價格。
function calculate() {
var total = 0,
discount = 0
;
$('input[type="checkbox"]:checked').each(function() {
var val = parseFloat($(this).val());
if(discount === 0 || val < discount) {
discount = val; // keep track of minimum
}
total += val;
});
//return all three values in one object
return {
total: total,
discount: disount,
discountedPrice: (total - discount)
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.