javascript - Sum of two input value by jquery -


i have code :

function compute() {     if ($('input[name=type]:checked').val() != undefined) {         var = $('input[name=service_price]').val();         var b = $('input[name=modem_price]').val();         var total = + b;         $('#total_price').val(a + b);     } } 

in code want sum values of 2 text inputs , write in text input has id of "total"

my 2 numbers don't sum example :

service_price value = 2000 , modem_price=4000 in example total input value must 6000 20004000

your code correct, except adding (concatenating) strings, not adding integers. change code into:

function compute() {     if ( $('input[name=type]:checked').val() != undefined ) {         var = parseint($('input[name=service_price]').val());         var b = parseint($('input[name=modem_price]').val());         var total = a+b;         $('#total_price').val(a+b);     } } 

and should work.

here working example updates sum when value when checkbox checked (and if checked, value updated when 1 of fields changed): jsfiddle.


Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -