Javascript - returning concatenates instead of sum of variables from input

前端 未结 6 874
南笙
南笙 2021-01-23 07:12

I am trying to get values from input field using .each() and adding them to show the sum in another input value. But unfortunately in spite of using parseInt() I am

6条回答
  •  旧时难觅i
    2021-01-23 08:15

    What you're parsing line is saying is

    Add the totalValue to the string from the input, then parse the result.

    Which is why it's concatenating. Instead, you need to add the totalValue outside of the parseInt() so that it's saying

    Take the take the totalValue and add the parsed input value to it.

    Like this

    totalvalue = totalvalue + parseInt($(this).val());
    

    This can be further shortened to

    totalvalue += parseInt($(this).val());
    

提交回复
热议问题