jQuery 部分中输入值的jQuery总和
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14142011/
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
jQuery sum of input values in sections
提问by Coop
I'm trying to find the sum of input values within multiple sections. I've put my code so far below.
我试图找到多个部分中输入值的总和。我已经把我的代码放在下面了。
The HTML:
HTML:
<div class="section">
<input type="radio" name="q1" value="2"/>
<input type="radio" name="q2" value="0"/>
<input type="radio" name="q3" value="1"/>
<input type="radio" name="q4" value="3"/>
</div>
The jQuery:
jQuery:
$('.section').each(function(){
var totalPoints = 0;
$(this).find('input').each(function(){
totalPoints += $(this).val();
});
alert(totalPoints);
});
Please note this is a simplified version of the code I'm actually using. So I want this to alert 2 values (the sum of each section): 8 then 6. Instead I'm just getting a string of all the values. So the first section alerts 0143.
请注意,这是我实际使用的代码的简化版本。所以我希望这能提醒 2 个值(每个部分的总和):8 然后 6。相反,我只是得到所有值的字符串。所以第一部分警报 0143。
Any ideas how I get a cumulative sum instead of a string?
任何想法如何获得累积总和而不是字符串?
回答by Royi Namir
You are doing "1"+"1" and expect it to be 2 ( int)
你正在做 "1"+"1" 并期望它是 2 ( int )
it is not.
它不是。
a very quick (and notfully correct) solution is :
一个非常快速(且不完全正确)的解决方案是:
$('.section').each(function(){
var totalPoints = 0;
$(this).find('input').each(function(){
totalPoints += parseInt($(this).val()); //<==== a catch in here !! read below
});
alert(totalPoints);
});
catch ? why ?
抓住 ?为什么 ?
answer: You should alwaysuse radix cause if you dont , a leading zero is octal!
答案:如果不使用基数,则应始终使用基数,前导零是八进制!
parseInt("010") //8 ( ff)
parseInt("010") //10 ( chrome)
parseInt("010",10) //10 ( ff)
parseInt("010",10) //10 ( chrome)
well.... you get the idea. supply radix !
反正你懂这个意思。供应基数!
edit
编辑
final solution (using .each( function(index, Element) )
)
最终解决方案(使用.each( function(index, Element) )
)
$('.section').each(function(){
var totalPoints = 0;
$(this).find('input').each(function(i,n){
totalPoints += parseInt($(n).val(),10);
});
alert(totalPoints);
});
回答by Doguhan Okumus
Use parseFloat()or parseInt()
var totalPoints = 0;
$('.section input').each(function(){
totalPoints = parseFloat($(this).val()) + totalPoints;
});
alert(totalPoints);
回答by Anthony Grist
The value is stored as a string, so calling +=
is doing string concatenation. You want/need to treat it as a number, so it does addition. Use the parseInt()
function to convert it to a number:
该值存储为字符串,因此调用+=
是进行字符串连接。你想/需要把它当作一个数字,所以它做加法。使用parseInt()
函数将其转换为数字:
totalPoints += parseInt($(this).val(), 10);
回答by RainbowFish
The javascript function parseInt() should achieve what you require, here's a fiddle: http://jsfiddle.net/k739M/
javascript 函数 parseInt() 应该可以满足您的要求,这是一个小提琴:http: //jsfiddle.net/k739M/
And some formatted code:
还有一些格式化的代码:
$('.section').each(function(){
var totalPoints = 0;
$(this).find('input').each(function(){
totalPoints += parseInt($(this).val());
});
alert(totalPoints);
});?
Additionally, if you were to call $(each) on '.section input', you can reduce the amount of processing time (and code).
此外,如果您要在“.section input”上调用 $(each),您可以减少处理时间(和代码)。
var totalPoints = 0;
$('.section input').each(function(){
totalPoints += parseInt($(this).val());
});
alert(totalPoints);
回答by Mahak Choudhary
Use eval instead of parseInt
var a = "1.5";
var b = "2";
var c = parseInt(a) + parseInt(b);
console.log(c); //result 3
var a = "1.5";
var b = "2";
var c = eval(a) + eval(b);
console.log(c); //result 3.5 this is accurate