Javascript jquery的两个输入值的总和
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6552959/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sum of two input value by jquery
提问by Ebad ghafoory
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"
在我的代码中,我想要两个文本输入的总和值,并写入一个 ID 为“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
service_price value = 2000
并且modem_price=4000
在本实施例中总输入值必须是6000,但它是20004000
回答by Tadeck
Your code is correct, except you are adding (concatenating) strings, not adding integers. Just change your code into:
您的代码是正确的,除了您添加(连接)字符串,而不是添加整数。只需将您的代码更改为:
function compute() {
if ( $('input[name=type]:checked').val() != undefined ) {
var a = parseInt($('input[name=service_price]').val());
var b = parseInt($('input[name=modem_price]').val());
var total = a+b;
$('#total_price').val(a+b);
}
}
and this should work.
这应该有效。
Here is some working example that updates the sum when the value when checkbox is checked (and if this is checked, the value is also updated when one of the fields is changed): jsfiddle.
这是一些工作示例,当复选框被选中时更新总和(如果选中,则当字段之一更改时该值也会更新):jsfiddle。
回答by sorpigal
Because at least one value is a string the + operator is being interpreted as a string concatenation operator. The simplest fix for this is to indicate that you intend for the values to be interpreted as numbers.
因为至少有一个值是字符串,所以 + 运算符被解释为字符串连接运算符。对此最简单的解决方法是表明您打算将值解释为数字。
var total = +a + +b;
and
和
$('#total_price').val(+a + +b);
Or, better, just pull them out as numbers to begin with:
或者,更好的是,将它们作为数字开始:
var a = +$('input[name=service_price]').val();
var b = +$('input[name=modem_price]').val();
var total = a+b;
$('#total_price').val(a+b);
See Mozilla's Unary +documentation.
请参阅 Mozilla 的Unary +文档。
Note that this is only a good idea if you know the value is going to be a number anyway. If this is user input you must be more careful and probably want to use parseInt
and other validation as other answers suggest.
请注意,如果您知道无论如何该值将是一个数字,这只是一个好主意。如果这是用户输入,您必须更加小心,并且可能希望parseInt
按照其他答案的建议使用和其他验证。
回答by Rodolfo
use parseInt as a = parseInt($('input[name=service_price]').val())
使用 parseInt 作为 a = parseInt($('input[name=service_price]').val())
回答by Vineesh Kalarickal
回答by Apurba Ranjan
<script>
$(document).ready(function(){
var a =parseInt($("#a").val());
var b =parseInt($("#b").val());
$("#submit").on("click",function(){
var sum = a + b;
alert(sum);
});
});
</script>
回答by Michael
Cast them to a Number
将它们投射到一个数字
$('#total_price').val(Number(a)+Number(b));
But before you do that
但在你这样做之前
if (!isNaN($('input[name=service_price]').val()) {...
回答by pradip kor
if in multiple class you want to change additional operation in perticular class that show in below example
如果在多个班级中您想更改以下示例中显示的特定班级中的附加操作
$('.like').click(function(){
var like= $(this).text();
$(this).text(+like + +1);
});