<input type="button" name="Submit" value="按钮" onclick="AddRecord();"> 
<TABLE  border="1" cellpadding="0" cellspacing="0" bordercolor="#999999"  id="activeTable"> 
  
</TABLE>  
<form name="form2" method="post" action=""> 
  <table width="560" border="0" cellspacing="0" cellpadding="0"> 
  <tr> 
    <td>小计 </td> 
    <td>总数量; 
    
        <input name="sumsl" type="text" id="sumsl"> 
      
    <div id="sumsl"> </div> </td> 
    <td>&nbsp; </td> 
    <td>&nbsp; </td> 
    <td>&nbsp; </td> 
    <td>&nbsp; </td> 
  </tr> 
</table>   <script>  
  var sumsl=0; 
  var count=0; 
  var abc=0; 
  function  AddRecord()  
  { 
  count++; 
  var  row  =  activeTable.insertRow(activeTable.rows.length);//id=recordTable    
  var  col  =  row.insertCell(0);  
  var  i  =  row.rowIndex;   col.innerHTML  =  " <input  type='text'  name='bw"+  i  +  "'  value='bw"+  i  +  "'  style='width:60px;' >";  
    col  =  row.insertCell(1);    
  col.innerHTML  =  " <input  type='text'  name='mc"+    i  +"'  value=''  style='width:60px;' >";  
    col  =  row.insertCell(2);    
  col.innerHTML  =  " <input  type='text'  name='sl"+    i  +"'  value='1' style='width:60px;'  id ='sl"+i+"' >";  
    col  =  row.insertCell(3);    
  col.innerHTML  =  " <input  type='text'  name='sf"+    i  +"'  value='' style='width:60px;' >";  
    col  =  row.insertCell(4);    
  col.innerHTML  =  " <input  type='text'  name='ljjg"+    i  +"'  value='' style='width:60px;'>";  
    col  =  row.insertCell(5);    
  col.innerHTML  =  " <input  type='text'  name='cz"+    i  +"'  value='' style='width:60px;' >";  
    col  =  row.insertCell(6);    
  col.innerHTML  =  " <textarea name='bz"+    i  +"' value='' style='width:60px;'> </textarea>";  
  col  =  row.insertCell(7);    
  col.innerHTML  =  " <input  type='button'  name='texClassOne"+    i  +"'  value='删除'  onclick='this.parentNode.parentNode.parentNode.deleteRow(this.parentNode.parentNode.rowIndex)'>";      }   </script> 
  <INPUT TYPE="button" VALUE="求和" ONCLICK="fun()">
  </form> 
</body> 
<SCRIPT LANGUAGE="JavaScript">
function fun()
{
var input = document.getElementById("activeTable").getElementsByTagName("input");
var total = 0;
for(var i=0;i<input.length ;i++ )
{
if(input[i].type=="text" && input[i].name.indexOf("sl")>-1)
{
total += isNaN(parseInt(input[i].value))?0:parseInt(input[i].value);
}
}
document.form2.sumsl.value = total
}
</SCRIPT>try

解决方案 »

  1.   

    function Sum()
    {
    var oTable = document.getElementById("表格ID");
    var iRowCount = oTable.rows.length;
    var iSum = 0;
    for(var i=0 ; i<iRowCount ; i++)
    {
    try
    {
    var iValue = parseInt(oTable.rows[i].cells["sli所在的CELL索引"].childNodes[0].value);
    iSum = iSum+iValue;
    }
    catch
    {

    }
    }
    return iSum;
    }
      

  2.   

    注:
     如果要求sl"+i+"与ljjg"+i+"的积再求和则可以这样,供大家参考[neo_yoho 写的]
    <SCRIPT LANGUAGE="JavaScript">
    function fun() 

    var rows = document.getElementById("activeTable").rows; 
    var total = 0; 
    for(var i=0;i<rows.length ;i++ ) 

    var input = rows[i].getElementsByTagName("input"); 
    var sl = 0,ljjg=0; 
    for(var j=0;j<input.length ;j++ ) 

    if(input[j].type=="text" && input[j].name.indexOf("sl")>-1) 
    sl = isNaN(parseInt(input[j].value))?0:parseInt(input[j].value); 
    if(input[j].type=="text" && input[j].name.indexOf("ljjg")>-1) 
    ljjg = isNaN(parseInt(input[j].value))?0:parseInt(input[j].value); 

    total += sl*ljjg 
    } document.form2.sumsl.value = total; 
    }</SCRIPT>