jquery - Get value from input, result as NaN -
jquery - Get value from input, result as NaN -
this weird, i'm trying create simple calculation, i'm not able value inputs reason, , it's working specific inputs, not everyone.
this way i'm trying value:
var days = parseint($('#days').val()); var dayspayment = parseint($('#days-payment').val());
but it's not working, though other inputs working fine.
you can see on jsfiddle here: http://jsfiddle.net/jeu4l/
you need retrieve values fields within click handler. @ moment getting them on load when blank, hence calculation yields nan
. also, should include radix on parseint
calls. seek this:
$(document).ready(function () { var $execute = $('#execute'); var $total = $('#total'); var $totalnds = $('#total-nds'); $execute.on('click', function () { var payment = parseint($('#payment').val().replace(/ /g, ""), 10); var nds = parseint($('#nds').val(), 10); var days = parseint($('#days').val(), 10); var dayspayment = parseint($('#days-payment').val(), 10); var t1 = payment / 100; var t2 = t1 * nds; var t3 = payment - t2; var t4 = t3 / days; var t5 = t4 * dayspayment; $total.val(t5); }); });
updated fiddle
i changed variable names utilize usual js , jquery conventions (ie. variables containing jquery objects prefixed $
, variable names should camel-cased).
jquery
Comments
Post a Comment