0

这是我的 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 个静态且永不改变的值。示例就像 20 美元的固定费用,始终在 html 中查看并将其自身添加到最终总数中。

有什么建议么?

4

1 回答 1

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;
}
于 2013-04-30T16:29:12.660 回答