如何将2个值添加到以下等式中?

时间:2013-04-30 16:23:01

标签: javascript math

这是我的javascript代码,它添加了一些用户选择的值,然后从总数中减去最小值:

 <script language="JavaScript"> 

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.grad_enroll_form.total.value = total;
    document.querySelector("#total").innerHTML = total;
    document.querySelector("#discount").innerHTML = discountAmount;
    document.getElementById("FinalTotal").value = withDiscount;
}

</script>

这就是我在页面上引用它的方式:

 <p style="font-size:15px"> <strong>Section, Division, Forum Charges: $ <span id="total">0</span></strong><br></p>
<p style="font-size:15px"> <strong>Discount:<span style="color:#F00"> $ </span><span style="color:#F00" id="discount">0</span></strong><br></p>
<p style="font-size:15px"><strong>Final total: $ <span id="newTotal">0</span></strong></p>

我想要做的是将2个值添加到静态且永不改变的等式中。例如,在html中随时查看20美元的固定费用,并将其添加到最终总数中。

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

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;

    //add the static prices here
    total+=price1;
    total+=price2;

    var withDiscount = total - min;
    var discountAmount = withDiscount - total;

    //document.grad_enroll_form.total.value = total;
    document.querySelector("#total").innerHTML = total;
    document.querySelector("#discount").innerHTML = discountAmount;
    document.getElementById("FinalTotal").value = withDiscount;
}
相关问题