سؤال

At first I have to apologize for my English. Im not native speaker.

Im a beginner and dont know JS or jQuery extensions well. Need to sum 3 input live changing values with unique ID.

I wish sum these values live too. So i have to write something like

total = $('#sumOne').value+$('#sumTwo').value+$(#sumThree).value

As I said, i really dont know JS, i know that the row above is absolutely incorrect.

Could anyone help me and write some example of script?

Thanks a lot for your time and advice.

هل كانت مفيدة؟

المحلول

parseInt($('#sumOne').val())+parseInt($('#sumTwo').val())+parseInt($(#sumThree).val());

You want:

this.value = ''; // straight JS, no jQuery

or

$(this).val(''); // jQuery

With $(this).value = '' you're assigning an empty string as the value property of the jQuery object that wraps this -- not the value of this itself.

reference val()

نصائح أخرى

use .val() function to extract the value attribute of the jquery object. And do parseInt on the values before adding. Otherwise the values will get concatinated instead of addition.

var total = parseInt($('#sumOne').val())+parseInt($('#sumTwo').val())+parseInt($(`#sumThree`).val());

You need to use .val()

 parseFloat($('#sumOne').val()) + parseFloat($('#sumTwo').val()) + parseFloat($(#sumThree).val())

Also convert values to float, using parseFloat

as reference to Rituraj's answer, its better to convert val() to int or float before adding them:

var total=parseFloat($('#sumOne').val())+parseFloat($('#sumTwo').val())+parseFloat($('#sumThree').val());
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top