除的时候判断一下文本框是否为空,
<input type="text" size="4"/>
<input type="text" size="4"/>
<input type="text" size="4"/>
<input type="text" size="4"/>
<input type="text" size="4"/>
<input type="button" value="计算" onclick="average()"/>
<script language="javascript">
<!--
function average()
{
var ts = document.getElementsByTagName( "input" );
var count = 0;
var cv = 0;
for(i=0;i<ts.length;i++)
{
if( ts[i].type != "button" && ts[i].value != "" )
{
count++;
cv += parseInt(ts[i].value);
}
}
alert(cv/count);
}
//-->
</script>
<input type="text" size="4"/>
<input type="text" size="4"/>
<input type="text" size="4"/>
<input type="text" size="4"/>
<input type="text" size="4"/>
<input type="button" value="计算" onclick="average()"/>
<script language="javascript">
<!--
function average()
{
var ts = document.getElementsByTagName( "input" );
var count = 0;
var cv = 0;
for(i=0;i<ts.length;i++)
{
if( ts[i].type != "button" && ts[i].value != "" )
{
count++;
cv += parseInt(ts[i].value);
}
}
alert(cv/count);
}
//-->
</script>
解决方案 »
- 请教一个js数组遍历判断是否包含的问题
- 初学者求教javascript使用问题
- 按钮可用与禁用的问题
- 下拉列表选中项的问题
- 网页中skype的判断自动安装问题
- 用dreamweaver做弹出式菜单,如何设置弹出菜单的背景图呢
- showmodledialog加codethat的tree的用法疑难??
- 请问 谁能详细解释 match 和 replace 的规则??????????????????
- <input 数组,事件中如果判断是第几个?
- Object 的 Attribute 和 Properties 有什么不同?
- 如何在htm页面中找到指定的元素并修改它的属性?
- <td>中的自动换行问题,在线等
<input name="1" type="text" value="" size="8" maxlength="5">
<input name="2" type="text" value="" size="8" maxlength="5">
<input name="3" type="text" value="" size="8" maxlength="5">
<input name="4" type="text" value="" size="8" maxlength="5">
<input name="5" type="text" value="" size="8" maxlength="5">
<input name="2" type="text" value="" size="8" maxlength="5">
<input name="3" type="text" value="" size="8" maxlength="5">
<input name="4" type="text" value="" size="8" maxlength="5">
<input name="5" type="text" value="" size="8" maxlength="5">
<input type="button" value="计算" onclick="average()"/>
<script language="javascript">
<!--
function average()
{
var ts = document.getElementsByTagName( "input" );
var count = 0;
var cv = 0;
for(i=0;i<ts.length;i++)
{
if( ts[i].name.substring(0,4) == "" && ts[i].value != "" )
{
alert(ts[i].name.substring(0,4))
count++;
cv += parseInt(ts[i].value);
}
}
alert(cv/count);
}
//-->
</script>
<input name="1" type="text" value="" size="8" maxlength="5">
<input name="2" type="text" value="" size="8" maxlength="5">
<input name="3" type="text" value="" size="8" maxlength="5">
<input name="4" type="text" value="" size="8" maxlength="5">
<input name="5" type="text" value="" size="8" maxlength="5">
<input type="button" value="计算" onclick="average()"/>
<script language="javascript">
<!--
function average()
{
var ts = document.getElementsByTagName( "input" );
var count = 0;
var cv = 0;
for(i=0;i<ts.length;i++)
{
if( ts[i].name.substring(0,4) == "" && ts[i].value != "" )
{
count++;
cv += parseInt(ts[i].value);
}
}
alert(cv/count);
}
//-->
</script>