日期:2014-05-16  浏览次数:20381 次

有几数组表单,js如何获得数组并动态相加输出到文本框
<input   name= "fee1[] "> <input   name= "fee2[] ">
<input   name= "fee1[] "> <input   name= "fee2[] ">
<input   name= "fee1[] "> <input   name= "fee2[] ">
<input   name= "fee ">
功能是js获得数组相加并动态在fee的文本框输出
fee.value=fee1[0].value+fee2[0].value+fee1[1].value+fee2[1].value......

------解决方案--------------------
var n=0;
var a = document.getElementsByName( "fee1[] ");
for(var i=0; i <a.length; i++)
{
if(!isNaN(a.value)) n += parseFloat(a.value);
}
var a = document.getElementsByName( "fee2[] ");
for(var i=0; i <a.length; i++)
{
if(!isNaN(a.value)) n += parseFloat(a.value);
}
document.getElementsByName( "fee ")[0].value = n==0 ? " " : n;

------解决方案--------------------
<input name= "fee1[] "> <input name= "fee2[] ">
<input name= "fee1[] "> <input name= "fee2[] ">
<input name= "fee1[] "> <input name= "fee2[] ">
<input name= "fee ">
<input type= "button " value= "累加 " onclick= "sum() ">
<script>
function sum()
{
o = document.getElementsByTagName( "INPUT ");
total = 0;
for(c=0;c <o.length;c++)
{
if(/^fee[0-9]+\[\]$/.test(o[c].name))
{
if(o[c].value!= " ") total+=parseInt(o[c].value);
}
}
document.getElementsByName( "fee ")[0].value = total;
}
</script>