减去和划分输入字段

时间:2014-03-30 17:34:24

标签: javascript jquery math input

尝试在一些输入字段上进行一些数学计算,这给了我一些问题

这是我要做的事情

  1. 从客户成本中的折扣显示中减去零售成本
  2. 然后从首付中扣除客户成本并按期限除以获得分期付款
  3. 如果已更改则选择菜单,然后重新计算分期付款
  4. 这是小提琴   http://jsfiddle.net/alienfactory1/GnTJ6/6/

     $( '#Discount' ).keyup(function() {
            paymentmethod();                         
        });
    
        $( '#finterm' ).change(function(){
                if ($('#finterm').val() == 12){
                      paymentmethod();
                } else if($('#finterm').val() == 18) {
                      paymentmethod();
                }
        });
    
         function paymentmethod(){
    
        var retailcost =  parseFloat($('#RetailCost').val().replace(/,/g, ''));
        var discount =  parseFloat($('#Discount').val());
        var downpay = parseFloat($('#DownPayment').val());
        var term = parseInt($('#finterm').val());
        var customercost =  $('#CustomerCost').val(retailcost - discount);
    
        var installment = customercost - downpay;
        var instald = installment / term;
        var test = $('#Instalments').val(instald); 
        $('#CustomerCost').autoNumeric('set', $(this).val());  
                         }
    
    Retail Cost
     <input type="text" class="form-control input-sm" placeholder="Retail Cost" id="RetailCost" readonly value="3,145.00"><br>
    Discount
    <input type="text" class="form-control input-sm" placeholder="Discount" id="Discount"><br>
    Cust Cost
    <input type="text" class="form-control input-sm" placeholder="Customer Cost" id="CustomerCost" readonly><br>
    Down Pay
    <input type="text" class="form-control input-sm" placeholder="Down Payment"   id="DownPayment" readonly><br>
    Term
    <select class="form-control input-sm" id="finterm">
    <option value="12">12</option>
    <option value="18">18</option>
    </select><br>
    Installments
    <input type="text" class="form-control input-sm" placeholder="Installments" id="Instalments" readonly>
    

2 个答案:

答案 0 :(得分:1)

我认为这可能是两个问题:

1)永远不会设置DownPayment。 2)在你的代码中,customercost是一个对象;

当我将DownPayment设置为2

时尝试此演示

http://jsfiddle.net/c6Xs5/1/

var retailcost =  parseFloat($('#RetailCost').val().replace(/,/g, ''));
var discount =  parseFloat($('#Discount').val());
var downpay = parseFloat($('#DownPayment').val());
var term = parseInt($('#finterm').val());
var customercost =  retailcost - discount;
$('#CustomerCost').val(customercost);                 
var installment = customercost - downpay;
var instald = installment / term;
var test = $('#Instalments').val(instald); 
$('#CustomerCost').autoNumeric('set', $(this).val());  
             }

答案 1 :(得分:0)

尝试使用parseIntparseFloat,以便JavaScript可以进行正确的数学运算。