Sum of two input value by jquery

2019-01-14 21:27发布

I have code :

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

In my code I want sum values of two text inputs and write in a text input that has an id of "total"

My two numbers don't sum together for example :

service_price value = 2000 and modem_price=4000 in this example total input value must be 6000 but it is 20004000

7条回答
狗以群分
2楼-- · 2019-01-14 21:57

use parseInt

   var total = parseInt(a) + parseInt(b);


    $('#total_price').val(total);
查看更多
登录 后发表回答