I have the following situation, I’m using jquery and I need to sum up some fields on my form.
I have found the NaN error in my subtotal field and total field.
I have tried everything possible to avoid this type of error, I just need the SUM on this field.
Everything in my form is working fine, only this 2 fields with a problem.
I’m using parseFloat() and no response. Only a field with NaN
Follow my javascript code:
$(document).ready( function() {
$('#valor, #taxa, #imposto, #envio, #taxa_adicional, #subtotal, #total').blur(function(){
// exemplo antigo var val = $('#valor').val();
var val = $('#valor').format({format:"#,###.00", locale:"br"});
var tax = $('#taxa').format({format:"#,###.00", locale:"br"});
var imp = $('#imposto').format({format:"#,###.00", locale:"br"});
var env = $('#envio').format({format:"#,###.00", locale:"br"});
var xat = $('#taxa_adicional').format({format:"#,###.00", locale:"br"});
if(val == "") val = 0;
if(tax == "") tax = 0;
if(imp == "") imp = 0;
if(env == "") env = 0;
if(xat == "") xat = 0;
var subtotal = parseFloat("val") + parseFloat("tax") + parseFloat("imp") + parseFloat("env");
var total = parseFloat(val) + parseFloat(tax) + parseFloat(imp) + parseFloat(env) + parseFloat(xat);
$('#subtotal').format({format:"#,###.00", locale:"br"});
$('#total').val(total);
})
});
Thanks in advance for any help on this matter! :-/
WARNING: I’m using a plugin called:
jquery.numberformatter – Formatting/Parsing Numbers in jQuery Written by Michael Abernethy
HTML:
JavaScript:
and then:
Live demo: http://jsfiddle.net/U9V6x/