尝试在一些输入字段上进行一些数学计算,这给了我一些问题
这是我要做的事情
$( '#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>
答案 0 :(得分:1)
我认为这可能是两个问题:
1)永远不会设置DownPayment。 2)在你的代码中,customercost是一个对象;
当我将DownPayment设置为2
时尝试此演示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)
尝试使用parseInt
和parseFloat
,以便JavaScript可以进行正确的数学运算。