javascript 中实现多个文本框相加的计算方式代码如下:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<link rel="stylesheet" href="finance.css">
<title>合计</title>
</head>
<body background="image/beg1.gif" leftmargin="0" topmargin="0">
<br>
<form id="frmxx" name="frmxx" action="Manager_F_budgeteeringSave.asp" method=post >
<table width="800" border="0" align=center cellpadding="0" cellspacing="0" class="tableframe">
<tr align=left class=listrow1> <td class=listcellrow nowrap align=right><div align="center"> <input name="txtTest1" type="text" value="120" size="20">
</div></td> </tr>
<tr align=left class=listrow1_1> <td class=listcellrow nowrap align=right><div align="center"> <input name="txtTest2" type="text" value="0" size="20">
</div></td> </tr>
<tr align=left class=listrow1> <td class=listcellrow nowrap align=right><div align="center"> <input name="txtTest3" type="text" value="0" size="20">
</div></td> </tr>
<tr align=left class=listrow1_1> <td class=listcellrow nowrap align=right><div align="center"> <input name="txtTest4" type="text" value="0" size="20">
</div></td> </tr>
<tr align=left class=listrow1> <td class=listcellrow nowrap align=right><div align="center"> <input name="txtTest5" type="text" value="0" size="20">
</div></td> </tr>
<tr align=left class=listrow1_1> <td class=listcellrow nowrap align=right><div align="center"> <input name="txtTest6" type="text" value="0" size="20">
</div></td> </tr>
<tr class=listrow1_1> <td colspan="9" nowrap class=listcellrow> <div align="center">合计值: <input name="sumdata" type="text" value="0" size="20" onfocus="sumhe()"> </div></td> </tr>
</table>
</form>
<script>
//window.onload=sumhe;
window.document.onkeyup=sumhe;
function sumhe(){
var lens,kk;
var j=0;
kk=0;
lens=document.frmxx.elements.length-1;
for(i=1;i<=6;i++){
kk+=parseFloat(eval('document.frmxx.txtTest'+i+'').value);
if (parseFloat(eval('document.frmxx.txtTest'+i+'').value)>0)
{j+=1;
}
}
document.frmxx.sumdata.value=(kk/j).toFixed(4);
}
</script>
</body>
</html>