简体   繁体   English

Javascript 显示 50 个文本框值的总和

[英]Javascript to display sum of 50 textboxes values

I have around 50 textboxes in my webpage, where user can enter numeric values.我的网页中有大约 50 个文本框,用户可以在其中输入数值。 As they enter the numbers I have to display the total of all the numbers in another label.当他们输入数字时,我必须显示另一个 label 中所有数字的总和。

Is there an AJAX or Javascript solution?是否有 AJAX 或 Javascript 解决方案?

So can someone please help me to find what is the best way to achieve this?那么有人可以帮我找到实现这一目标的最佳方法吗?

With jQuery配jQuery

var total = 0;
$('input:text').each(function() {
    total = total + parseInt($(this).val());
});

With plain JavaScript带普通 JavaScript

var total = 0;
var inputs = document.getElementsByTagName('input');
for(var i=0; i < inputs.length; i++)
{
    var input = inputs[i]
    if (input.type == 'text') {
         total = total + parseInt(input.value);
    }
}

What you would need to do is set an event listener (onkeyup maybe) on every box that calls a single function.您需要做的是在每个调用单个 function 的框上设置一个事件侦听器(可能是 onkeyup)。 That function iterates over all the boxes and sets the appropriate value. function 遍历所有框并设置适当的值。

http://jsfiddle.net/HqUDw/ http://jsfiddle.net/HqUDw/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 添加两个文本框中的值,并在第三个文本框中显示总和 - Add values from two textboxes and display the sum in third textbox 在单个文本框中显示查询值 - Display Query Values in Individual Textboxes 如何在C#WinForm文本框中显示/激发SQL Server数据库中的数据(列总和) - How to display/ fire data (sum column values) from SQL Server database in C# WinForm textBoxes 两个文本框,它们的值之和是互补的 - Two textboxes which sum of values are complementary 从两个文本框中添加值并在第三个文本框 3 中显示总和,但在第三个文本框中显示第一个或第二个文本框的值 - Add values from two textboxes and display the sum in third textbox 3 but value of 1st aor 2nd textbox show in third textbox 如何汇总和显示文本框每一列的结果? - How do I sum and display the result of each column of textboxes? 设置使用Javascript创建的文本框的值 - Setting Values of textboxes that were created using Javascript 如何获取更新的 2 个文本框中的值的总和 C# - how to get the sum of values in 2 textboxes that gets updated C# 将动态创建的文本框的值插入sqlserver并在gridview中显示 - Insert values of dynamically created textboxes into sqlserver and display in gridview 如何从JavaScript中的GridView的templatefield文本框中获取值? - How to get values from a GridView's templatefield textboxes in JavaScript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM