我将两个字段一起添加以获得总数但是当我输入第一个字段时,总字段显示NaN,我不需要它。我已经尝试了5种不同的选项来解决这个问题,但是当函数运行时似乎没有删除NaN。
$j(function() {
var new_fees = 0.00;
var referral_fees = 0.00;
$j('#new_fees').keyup(function() {
updateTotal();
});
$j('#referral_fees').keyup(function() {
updateTotal();
});
var net_fees=parseFloat($j('#net_fees').val());
if(isNaN(net_fees)){
net_fees=0;
}
var updateTotal = function () {
new_fees = parseFloat($j('#new_fees').val());
referral_fees = parseFloat($j('#referral_fees').val());
$j('#net_fees').val(new_fees + referral_fees);
var output_total = $j('#net_fees');
var total = new_fees + referral_fees;
output_total.val(total);
};
});
以下是fiddle link,但它似乎并不像我在页面上那样在小提琴中工作
答案 0 :(得分:1)
我更新了您的“updateTotal”方法,请尝试一下。
git checkout -b myBranch origin/TheOtherBranch
答案 1 :(得分:0)
试试这个
var new_fees = $("#new_fees").val();
var referral_fees = $("#referral_fees").val();
$('#new_fees').keyup(function() {
updateTotal();
});
$('#referral_fees').keyup(function() {
updateTotal();
});
var updateTotal = function () {
new_fees = parseFloat($('#new_fees').val());
referral_fees = parseFloat($('#referral_fees').val());
if(isNaN(new_fees))
net_fees=0;
if(isNaN(referral_fees))
referral_fees=0;
$('#net_fees').val(new_fees + referral_fees);
var output_total = $('#net_fees');
var total = new_fees + referral_fees;
if(isNaN(total ))
total=0.00;
output_total.val(total);
};
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="col-xs-12 col-sm-2 nopadding">
<label for="template_range" class="col-xs-11 col-sm-11 col-md-11 col-lg-11 nopadding">New Fees<br><small>(net)</small></label>
<input type="text" class=" col-xs-5 form-control" id="new_fees" placeholder="0.00">
</div>
<div class="col-xs-12 col-sm-2 nopadding">
<label for="template_range" class="col-xs-11 col-sm-11 col-md-11 col-lg-11 nopadding">Referral Fees<br> <small>(net)</small></label>
<input type="text" class="col-xs-5 form-control" id="referral_fees" placeholder="0.00" >
</div>
<div class="col-xs-12 col-sm-2 nopadding">
<label for="template_range" class="col-xs-11 col-sm-11 col-md-11 col-lg-11 nopadding">Total Fees <br><small>(net)</small></label>
<input type="text" class="col-xs-5 form-control" value="0.00" id="net_fees" readonly>
</div>
<div class="col-xs-12 col-sm-2 nopadding">
<label for="template_range" class="col-xs-11 col-sm-11 col-md-11 col-lg-11 nopadding">Total Fees <br><small>(gross)</small></label>
<input type="text" class="col-xs-5 form-control" value="0.00" id="gross_fees" readonly>
</div>
答案 2 :(得分:0)
如果parseFloat返回NaN
,则默认为0var updateTotal = function () {
new_fees = parseFloat($j('#new_fees').val()) || 0;
referral_fees = parseFloat($j('#referral_fees').val()) || 0;
$j('#net_fees').val(new_fees + referral_fees);
var output_total = $j('#net_fees');
var total = new_fees + referral_fees;
output_total.val(total);
};