Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/455.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用javascript对输入字段求和_Javascript_Sum_Each - Fatal编程技术网

使用javascript对输入字段求和

使用javascript对输入字段求和,javascript,sum,each,Javascript,Sum,Each,如何自动(通过每个函数)对字段求和,以对一些输入字段求和,如下所示: <input name="price_1" id="price_1" type="text" value="50" /> <input name="price_2" id="price_2" type="text" value="40" /> <input name="price_3" id="price_3" type="text" value="20" /> <input name

如何自动(通过每个函数)对字段求和,以对一些输入字段求和,如下所示:

<input name="price_1" id="price_1" type="text" value="50" />
<input name="price_2" id="price_2" type="text" value="40" />
<input name="price_3" id="price_3" type="text" value="20" />
<input name="price_4" id="price_4" type="text" value="10" />
<input name="price_5" id="price_5" type="text" value="80" />

如果有人有什么建议,非常感谢?

关于


var inputs=document.getElementsByTagName(“输入”);
函数sumIt(){
var sum=Array.prototype.reduce.call(输入,函数(a,b){
返回a+b浮点数(b值);
}, 0);
控制台日志(总和);
}
Array.prototype.forEach.call(输入,函数(输入){
input.addEventListener(“keyup”,sumIt,false);
});
使用jQuery

<script src="{path to jquery}/resources/js/vendor/jquery-1.9.0.min.js"></script>
<script>
$(document).ready(function() {
 var total = 0;
 $.each($('input'), function(){
   total += parseInt($(this).val(), 10); //use radix 10 to ensure correct parseInt val
 });
 console.log(total);
});
</script> 

$(文档).ready(函数(){
var合计=0;
$。每个($('input'),函数(){
total+=parseInt($(this.val(),10);//使用基数10确保parseInt val正确
});
控制台日志(总计);
});

选择输入。。。获取值。。。添加它们。有什么问题吗?好的,我写得不够清楚。我每行有不止一个输入字段,所以我需要从特定的输入字段中获取值。对于本例,它是id=“price_[]”。您确定要求和的值是整数吗?为什么要用jquery,一个完整的库来完成如此琐碎的任务。哈!帮自己一个忙,将jQuery添加到项目中。它超轻量,全球超过一半的web开发人员使用它。是的,您可以不使用,并且您可能希望检查和跳过非数字的求和值。只是为了一个简洁易读的解决方案。你的也很酷。我对jquery没有任何问题,我喜欢它,并在适当的时候使用它。现在似乎每个人都试图在任何事情上使用它,而javascript的基本艺术却被忽视了。Javascript很漂亮,功能强大,可读性也很好,如果没有它,jquery会怎么样作为比较,看看我们建议的这个jsperf(),纯javascript还向每个输入元素添加了一个事件监听器。
<script src="{path to jquery}/resources/js/vendor/jquery-1.9.0.min.js"></script>
<script>
$(document).ready(function() {
 var total = 0;
 $.each($('input'), function(){
   total += parseInt($(this).val(), 10); //use radix 10 to ensure correct parseInt val
 });
 console.log(total);
});
</script>