用函数值除以数字

时间:2014-02-24 20:51:04

标签: javascript function input divide

我希望能够从calcOrderTotal输入中获取值,然后将其除以并在另一个输入中显示分割的输出(例如,显示订单总价格,然后订单总共36个月租赁价格是)。我试图用“calc36Month”函数来做这件事,但我知道这不对。

function calcOrderTotal() {

var orderTotal = 0;

var productSubtotal = $("#product-subtotal").val() || 0;
var serverPrice = $('.server-radio:checked').val() || 0;
var equipmentPrice = $('.equipment-radio:checked').val() || 0;
var underTotal = $("#under-box").val() || 0;

var orderTotal = parseFloat(CleanNumber(productSubtotal)) + parseFloat(CleanNumber(serverPrice)) + parseFloat(CleanNumber(equipmentPrice));    

$("#order-total").val(CommaFormatted(orderTotal));

$("#fc-price").attr("value", orderTotal);

}

然后将calcOrderTotal函数重定向到此HTML输入并显示美元值(这确实有效):

<input type="text" class="total-box" value="$0" id="order-total" disabled="disabled" name="order-total"></input>

我希望能够获取OrderTotal美元值并将其除以36个月,并将36个月的租赁价值输入另一个输入。这是我正在寻找的一个例子(我知道这不起作用):

function calc36Month() {

    var 36Month = 0;

    var orderTotal = $("#order-total").val() || 0;

    var 36Month = parseFloat(CleanNumber(orderTotal)) / 36;    

    $("#36-monthly-total").val(CommaFormatted(36Month));

    $("#fc-price").attr("value", 36Month);

}

我该怎么做?

1 个答案:

答案 0 :(得分:0)

这里你去:

function calcOrderTotal() {
    var orderTotal = 0;

    var productSubtotal = $("#product-subtotal").val() || 0;
    var serverPrice = $('.server-radio:checked').val() || 0;
    var equipmentPrice = $('.equipment-radio:checked').val() || 0;
    var underTotal = $("#under-box").val() || 0;

    var orderTotal = parseFloat(CleanNumber(productSubtotal)) + parseFloat(CleanNumber(serverPrice)) + parseFloat(CleanNumber(equipmentPrice));    

    $("#order-total").val(CommaFormatted(orderTotal));
    $("#fc-price").attr("value", orderTotal);
    if (orderTotal > 0) {
        calcMonthly(orderTotal);
    }
}

编辑:根据请求编辑。

function calcMonthly(total) {
    var pmt1 = total / 36;
    var pmt2 = total / 24;
    var pmt3 = total / 12;
    $("#monthly-36").val(CommaFormatted(pmt1));
    $("#monthly-24").val(CommaFormatted(pmt2));
    $("#monthly-12").val(CommaFormatted(pmt3));
    //$("#fc-price").attr("value", pmt1); // what is the purpose of this?
}

避免使用数字作为变量名,元素ID或CSS类,或者使用数字开头任何上述引用。用字母开始所有变量名,ID和类。