2017-03-23 4 views
0

Wie kann ich die Summe der Zeilen und Spalten in einer Tabelle mit Javascript berechnen. Ich berechne die Spalten mit dem Code unterSumme der Zeilen und Spalten in einer Tabelle von Javascript

1) Ich wiederhole den Code 3 mal für 3 Spalten mit ein paar Änderungen. Ich denke, es gibt einen anspruchsvolleren Weg. Wie ?

2) Wie kann ich die Summe der Zeilen in dieser Tabelle berechnen?

<HEAD> 


     <script type="text/javascript"> 
    function findTotalcol(){ 
    var arr = document.getElementsByName('col1'); 
    var tot=0; 
    for(var i=0;i<arr.length;i++){ 
    if(parseInt(arr[i].value)) 
    tot += parseInt(arr[i].value); 
      } 
    document.getElementById('totalcol1').value = tot; 
    } 
    function findTotalcol2(){ 
    var arr = document.getElementsByName('col2'); 
    var tot=0; 
    for(var i=0;i<arr.length;i++){ 
    if(parseInt(arr[i].value)) 
    tot += parseInt(arr[i].value); 
    } 
     document.getElementById('totalcol2').value = tot; 
     } 

    function findTotalcol3(){ 
    var arr = document.getElementsByName('col3'); 
    var tot=0; 
    for(var i=0;i<arr.length;i++){ 
    if(parseInt(arr[i].value)) 
    tot += parseInt(arr[i].value); 
    } 
    document.getElementById('totalcol3').value = tot; 
      } 
    </script> 


     </HEAD>          
     <BODY >     

     <TABLE align="center" width="100%" border="1" cellspacing="0"> 
      <TR>   <!-- 1 --> 
      <TD align="center" width="25%"> 

      <input onblur="findTotalcol()" type="text" name="col1" /><br>   

      </TD> 
       <TD align="center" width="25%"> 

      <input onblur="findTotal2()" type="text" name="col2" /><br>   

      </TD> 

         <TD align="center" width="25%"> 

      <input onblur="findTotalcol3()" type="text" name="col3" /><br>   

      </TD> 

        <TD align="center" width="25%"> 

      Total row:<input onblur="" type="text" name="totalrow1" /><br>   

      </TD> 

      </TR>  
        <TR> <!-- 2 --> 
      <TD align="center" width="25%"> 

     <input onblur="findTotalcol()" type="text" name="col1" /><br>   

      </TD> 

        <TD align="center" width="25%"> 

      <input onblur="findTotalcol2()" type="text" name="col2" /><br>   

      </TD> 

         <TD align="center" width="25%"> 

      <input onblur="findTotalcol3()" type="text" name="col3" /><br>   

      </TD>  
         <TD align="center" width="25%"> 

      Total row:<input onblur="" type="text" name="totalrow2" /><br>   

      </TD> 


      </TR> 

        <TR> <!-- 3 --> 
      <TD align="center" width="30%"> 

     <input onblur="findTotalcol()" type="text" name="col1" ><br>  

      </TD> 


      <TD align="center" width="30%"> 

      <input onblur="findTotalcol2()" type="text" name="col2" /><br>   

      </TD> 

         <TD align="center" width="30%"> 

      <input onblur="findTotalcol3()" type="text" name="col3" /><br>   

      </TD>  

          <TD align="center" width="25%"> 

     Total row: <input onblur="" type="text" name="totalrow3" /><br>   

      </TD> 

      </TR> 
          <TR> <!-- total column --> 
      <TD align="center" width="30%"> 

     Total: <input type="text" name="totalcol1" id="totalcol1"/>  

      </TD> 

        <TD align="center" width="30%"> 

     Total: <input type="text" name="totalcol2" id="totalcol2"/>  

      </TD> 

       <TD align="center" width="30%"> 

    Total: <input type="text" name="totalcol3" id="totalcol3"/>  

      </TD> 


      </TR> 

      </TABLE> 

Antwort

1

JS

function findTotal(colName, totalColName) { 
    var arr = document.getElementsByName(colName); 
    var tot = 0; 
    for(var i = 0; i < arr.length; i++) { 
     if(parseInt(arr[i].value)) { 
      tot += parseInt(arr[i].value); 
     } 

     document.getElementById(totalColName).value = tot; 
    } 
} 

HTML

<input onblur="findTotal('col2', 'totalCol2')" type="text" name="col2" /> 
+0

Vielen Dank für Ihre Hilfe. Aber es hat nicht funktioniert. Ich schreibe Tags für jede Zelle wie unten. Wo ist mein Fehler?
etc ... – user1729894

Verwandte Themen