您好我正在尝试计算JS中的浮点值 我是我的小计,我想在该值中应用折扣
我这样做了:
$("#desconto").focusout(function(){
var subtotal = $('#subtotal').val();
var desconto = $('#desconto').val();
if(desconto > 0)
total = ((subtotal) - (desconto));
else
total = subtotal;
$('#total').val(total);
$('#totaldisabled').val(total);
});
但我的结果只是负数,我不知道如何解决这个问题,我也试过了
parseFloat(desconto.replace(/[^\d\.]/, '')
但不行,有人可以帮帮我!? 谢谢!
答案 0 :(得分:1)
而不是
var subtotal = $('#subtotal').val();
var desconto = $('#desconto').val();`
尝试使用:
var subtotal = parseFloat($('#subtotal').val());
var desconto = parseFloat($('#desconto').val());
答案 1 :(得分:0)
$("#desconto").focusout(function(){
var subtotal = $('#subtotal').val();
var desconto = $('#desconto').val();
subtotal = subtotal.replace('.' , '');
subtotal = parseFloat(subtotal.replace(',' , '.'));
desconto = desconto.replace('.' , '');
desconto = parseFloat(desconto.replace(',' , '.'));
if(desconto > 0)
total = (subtotal - desconto).toFixed(2);
else
total = subtotal.toFixed(2);
$('#total').val(total);
$('#totaldisabled').val(total);
});//end main
工作!让所有人感到高兴