<html>
<head>
<title>New Page 1</title>
</head>
<script>
function fn()
{for(var i=1;i<3;i++)'
   eval("f1.c"+i).value=eval("f1.a"+i).value*1+eval("f1.b"+i).value*1
}
</script>
<body>
<form name="f1">
<table border="1" width="46%">
  <tr>
    <td width="23%"><input type="text" name="a1" size="20" value="1"></td>
    <td width="77%"><input type="text" name="b1" size="20" value="1"></td>
    <td width="77%"><input type="text" name="c1" size="20"></td>
  </tr>
  <tr>
    <td width="23%"><input type="text" name="a2" size="20" value="2"></td>
    <td width="77%"><input type="text" name="b2" size="20" value="2"></td>
    <td width="77%"><input type="text" name="c2" size="20"></td>
  </tr>
</table>
<input type="button" name="bu" size="20" value="确认" onclick="fn()">
</form>
</body>
</html>

解决方案 »

  1.   

    上边的少了点东西
    <html>
    <head>
    <title>New Page 1</title>
    </head>
    <script>
    function fn()
    {for(var i=1;i<3;i++)//这里的3表示有2组,如果100组则为101
       eval("f1.c"+i).value=eval("f1.a"+i).value*1+eval("f1.b"+i).value*1
    }
    </script>
    <body>
    <form name="f1">
    <table border="1" width="46%">
      <tr>
        <td width="23%"><input type="text" name="a1" size="20" value="1"></td>
        <td width="77%"><input type="text" name="b1" size="20" value="1"></td>
        <td width="77%"><input type="text" name="c1" size="20"></td>
      </tr>
      <tr>
        <td width="23%"><input type="text" name="a2" size="20" value="2"></td>
        <td width="77%"><input type="text" name="b2" size="20" value="2"></td>
        <td width="77%"><input type="text" name="c2" size="20"></td>
      </tr>
    </table>
    <input type="button" name="bu" size="20" value="确认" onclick="fn()">
    </form>
    </body>
    </html>
      

  2.   

    假设网页中有一个100行,3列的表格,每个单元格内都有一个文本框,我想要将每行第1,2列的数据相加显示在第3行内。如何编写JavaScript自动处理每行的数据。
    <html>
    <head>
    <title>New Page 1</title>
    </head>
    <script>
    function fn()
    {
    var row = document.all.tab.rows;
    for(var i=0;i<row.length;i++)
    {
        row[i].childNodes[2].childNodes[0].value = parseInt(row[i].childNodes[0].childNodes[0].value) + parseInt(row[i].childNodes[1].childNodes[0].value);
    }
    }
    </script>
    <body>
    <form name="f1">
    <table border="1" width="46%" id="tab">
      <tr>
        <td width="23%"><input type="text" name="a1" size="20"></td>
        <td width="77%"><input type="text" name="b1" size="20"></td>
        <td width="77%"><input type="text" name="c1" size="20"></td>
      </tr>
      <tr>
        <td width="23%"><input type="text" name="a2" size="20"></td>
        <td width="77%"><input type="text" name="b2" size="20"></td>
        <td width="77%"><input type="text" name="c2" size="20"></td>
      </tr>
    </table>
    <input type="button" name="bu" size="20" value="确认" onclick="fn()">
    </form>
    </body>
    </html>