最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

calculate numbers in html table with javascript - Stack Overflow

programmeradmin1浏览0评论

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:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ".dtd">
<html xmlns="" >
<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

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:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3/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

Share Improve this question asked Jan 13, 2012 at 17:16 LazialeLaziale 8,22548 gold badges155 silver badges271 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 2

You can loop through each input element in the table and grab the 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.

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

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;
发布评论

评论列表(0)

  1. 暂无评论