javascript 用javascript计算html表中的数字

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8854543/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 04:47:08  来源:igfitidea点击:

calculate numbers in html table with javascript

javascripthtml

提问by Laziale

I am working on a project where I need to calculate some numbers in html table with javascript. The user can add as much numbers as they need and once they click a button, they'll need to see the end sum. I would like to know how I can do that with javascript, you can find the code below:

我正在做一个项目,我需要用 javascript 计算 html 表中的一些数字。用户可以根据需要添加任意数量的数字,一旦他们单击按钮,他们将需要查看最终总和。我想知道如何使用 javascript 做到这一点,您可以在下面找到代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>Javascript Calculator</title>

    <SCRIPT language="javascript">    
   function Calculate()
{
}
    function addRow()
    {
var table = document.getElementById('table');

var button = document.getElementsByTagName('input')[0];

button.onclick = function() {
    var clone = table.rows[table.rows.length - 2].cloneNode(true);
    clone.cells[0].firstChild.data =
        clone.cells[0].firstChild.data.replace(/(\d+):/,function(str,g1) {
                                                           return (+g1 + 1) + ':';
                                                        });
    table.tBodies[0].appendChild(clone);
};
 }
    </SCRIPT>
</head>

<input type=button value='Add Row' onclick="addRow()" />
<br /><br />
<table cellspacing=0 cellpadding=4 id="table">
<tbody>
<tr><td>Number 1:</td><td><input value=20 style="width:30px" type=text  maxlength=2/></td></tr>
<tr><td>Number 2:</td><td><input value=25 style="width:30px" type=text  maxlength=2/></td></tr>
</tbody>
<tfoot>
<tr><td style="border-top:solid 1px black;border-bottom:solid 1px black;">Sum:</td><td style="border-top:solid 1px black;border-bottom:solid 1px black;"><div>45</div></td></tr>
    </tfoot>
</table>
<input type=button value='Recalculate Sum' onclick="Calculate()" />
</body>
</html>

Any idea how I can solve this? Thanks in advance, Laziale

知道我该如何解决这个问题吗?提前致谢, Laziale

回答by Brigand

You can loop through each input element in the table and grab the value.

您可以遍历表中的每个输入元素并获取value.

function Calculate() {
    var table = document.getElementById('table');
    var items = table.getElementsByTagName('input');
    var sum = 0;
    for(var i=0; i<items.length; i++)
        sum += parseInt(items[i].value);
    var output = document.getElementById('sum');
    output.innerHTML = sum;
}

demo

演示



The only change I made to the HTML was giving an ID to the output div.

我对 HTML 所做的唯一更改是为输出 div 提供了一个 ID。

<div id="sum">45</div>

回答by caleb

use this to grab the inputs

使用它来获取输入

var inputs = document.getElementsByTagName('input'); 

this will grab all the inputs's, not just 'text' inputs, so you'll want to use something like this in the loop

这将获取所有输入,而不仅仅是“文本”输入,因此您需要在循环中使用类似的内容

if(inputs[i].type == 'text')
   total = total + parseInt(inputs[i].value);

and set the value like this

并像这样设置值

document.getElementById('displaySum').innerHTML = total;