<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<table width="80%" border="1" align="center" id="test">
<tr>
<td width="69">列值</td>
<td width="69">整数一</td>
<td width="69">整数二</td>
<td width="54">总和</td>
</tr>
<tr>
<td>1</td>
<td><input type="text" name="textfield" οnchange="count(this)"></td>
<td><input type="text" name="textfield3" οnchange="count(this)"></td>
<td><input type="text" name="textfield5" disabled></td>
</tr>
<tr>
<td>2</td>
<td><input type="text" name="textfield2" οnchange="count(this)"></td>
<td><input type="text" name="textfield4" οnchange="count(this)"></td>
<td><input type="text" name="textfield6"disabled></td>
</tr>
<tr>
<td>合计</td>
<td><input type="text" name="textfield2" disabled></td>
<td><input type="text" name="textfield4" disabled></td>
<td><input type="text" name="textfield6" disabled></td>
</tr>
</table>
<script type="text/javascript">
function count(inp) {
//算总和
var trObj = inp.parentElement.parentElement;
var sum = 0;
for (var i=1; i<trObj.children.length-1; i++) //循环input所在行的单元格(除去第一格和最后一格)
{
sum += trObj.childNodes[i].firstChild.value ? parseInt(trObj.childNodes[i].firstChild.value,10) : 0; //如果input值为空,则算作0
}
trObj.childNodes[trObj.children.length-1].firstChild.value = sum; //给最后一格内的input赋值
//算合计
var tab = document.getElementById("test");
for (var i=1; i<tab.rows[0].cells.length; i++) //循环列(除去第一列和最后一列)
{
var total = 0;
for (var j=1; j<tab.rows.length-1; j++) //循环行(除去第一行和最后一行)
{
total += tab.rows[j].cells[i].firstChild.value ? parseInt(tab.rows[j].cells[i].firstChild.value,10) : 0; //如果input值为空,则算作0
}
tab.rows[tab.rows.length-1].cells[i].firstChild.value = total; //给每列最下面一行的input赋值
}
}
</script>
</body>
</html>