js 多个文本框相加

本文介绍了一种使用JavaScript实现在网页上多个文本框输入数值并自动计算平均值的方法。该方法通过监听键盘事件实时更新结果显示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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> 


 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值