×

Loading...
Ad by
  • 推荐 OXIO 加拿大高速网络,最低月费仅$40. 使用推荐码 RCR37MB 可获得一个月的免费服务
Ad by
  • 推荐 OXIO 加拿大高速网络,最低月费仅$40. 使用推荐码 RCR37MB 可获得一个月的免费服务

我说这位 DX 怎么就不试试偶说的方法呢

本文发表在 rolia.net 枫下论坛<html>
<head>
<script language="javascript">
function go()
{
var tbl = document.forms[0].getElementsByTagName("TABLE")[0];
var trs = tbl.rows;
for (var i=0; i<trs.length; i++)
{
var allINPUTs = trs[i].getElementsByTagName("INPUT");
var rowsum = 0;
for (var j=0; j<allINPUTs.length; j++)
{
rowsum += parseInt(allINPUTs[j].value, 10);
}
trs[i].lastChild.firstChild.innerText = rowsum;
}
}
</script>
</head>
<body>
<form>
<table>
<tr>
<td><Input type="text" name="col1_1"></td>
<td><Input type="text" name="col2_1"></td>
<td><Input type="text" name="col3_1"></td>
<td><span></span></td>
</tr>
<tr>
<td><Input type="text" name="col1_2"></td>
<td><Input type="text" name="col2_2"></td>
<td><Input type="text" name="col3_2"></td>
<td><span></span></td>
</tr>
</table>
<input type="button" value="go" onclick="go()" />
</form>
</body>
</html>

说明:
0。如果这种方案能满足你的需求,则 input 不需要 id
1。没有写 error handling
2。如果不想要 button,可以考虑使用 expression....更多精彩文章及讨论,请光临枫下论坛 rolia.net
Report

Replies, comments and Discussions:

  • 工作学习 / IT技术讨论 / In the following form, How to use FOR LOOP (Java script) to sum the 2 rows respectively. I mean each row gets a sum. Please show me with code,Thank you very much.
    <form>
    <table>
    <tr><td><Input type="text" name="col1_1"></td>
    <td><Input type="text" name="col2_1"></td>
    <td><Input type="text" name="col3_1"></td>
    </tr>
    <tr><td><Input type="text" name="col1_2"></td>
    <td><Input type="text" name="col2_2"></td>
    <td><Input type="text" name="col3_2"></td>
    </tr>
    </table>
    </form>
    • In the above code, I can read form data like this: c=document.theform.col1_1.value, but how to make col1_1 increasing automatically,like col2_1,col3_1? Give me some advices please, Thank u very much.
    • 见内
      其实你想要知道的是怎么动态的求值,那么有一个JavaScript的函数Eval你就不能不知道,给你一个简单的例子:

      function countSum()
      {
      var nRow = 2;
      var nCol = 3;

      for (var i = 1; i <= nRow; i++)
      {
      var nSum = 0;

      for (var j = 1; j <= nCol; j++)
      {
      var strTextName = "document.forms[0].col" + j + "_" + i;
      var nNum = eval(strTextName + ".value");
      nSum = nSum + nNum;
      }

      alert(nSum);
      }
      }

      Good night.
      • The inside For Loop has some bugs.Could you check it? Thank you .
        for (var j = 1; j <= nCol; j++)
        {
        var strTextName = "document.forms[0].col" + j + "_" + i;
        var nNum = eval(strTextName + ".value");
        nSum = nSum + nNum;
        }
        • What bugs? detail message please, i'm ok here.
          • Really? With your code, an alert with "0" pop out twice when I intend to input value. Don't mention to get the result. Sorry to respond late, I went out for national day.
            • 呵呵,算了,给你把整个文件贴上来好了
              本文发表在 rolia.net 枫下论坛<html>
              <head><title>EVAL</title></head>

              <script language="JavaScript">
              function countSum()
              {
              var nRow = 2;
              var nCol = 3;

              for (var i = 1; i <= nRow; i++)
              {
              var nSum = 0;

              for (var j = 1; j <= nCol; j++)
              {
              var strTextName = "document.forms[0].col" + j + "_" + i;
              var nNum = eval(strTextName + ".value");
              nSum = parseInt(nSum) + parseInt(nNum);
              }

              alert("line_" + i + "'s sum: " + nSum);
              }
              }
              </script>

              <body>
              <form>
              <table>
              <tr><td><Input type="text" name="col1_1"></td>
              <td><Input type="text" name="col2_1"></td>
              <td><Input type="text" name="col3_1"></td>
              </tr>
              <tr><td><Input type="text" name="col1_2"></td>
              <td><Input type="text" name="col2_2"></td>
              <td><Input type="text" name="col3_2"></td>
              </tr>
              </table><br>
              <input type="button" name="sum" value=" SUM " onClick="countSum()">
              </form>
              </body>
              </html>

              1. 没有错误判断,只是纯粹演示EVAL的用法。
              2. 先在六个Input里面输入数字,然后按SUM键。
              3. 说点题外话,要想干这行,就得具备点探索精神和Debug的能力,别人给你一段代码,即使不work,也要自己先试着去找找原因,找到问题所在然后再尝试去解决,这样进步才快。更多精彩文章及讨论,请光临枫下论坛 rolia.net
              • Hi,Xanada. Thank you for your code. And your advice is sincere and right. But to be honest,
                I am not and won't be an IT guy. My major has connection with IT, and what i am doing is a small project(In my project, I use javascript,vbscript,asp,and even java). But for an outsider, I need learn everything from start, sometimes just too anxious to calm down to read books.I appreciate all the help I get here. This is really the best forum I've ever seen.And thank you all.
                • 收到,了解,箍蜡。
      • The problem here is actually how to make browser think the i and j in the field name(coli_j) is variables but not constant? How can I ?Thanks a lot
        • 给你个苯办法,动态生成该段javascript的code。
          • Could you tell me what the dummy way is ? Thanks.
            • 你不会用document.write?
            • 不过这样其实没有什么必要,不如试试form.elements[“col” + i + "_" + j].value
    • 我说这位 DX 怎么就不试试偶说的方法呢
      本文发表在 rolia.net 枫下论坛<html>
      <head>
      <script language="javascript">
      function go()
      {
      var tbl = document.forms[0].getElementsByTagName("TABLE")[0];
      var trs = tbl.rows;
      for (var i=0; i<trs.length; i++)
      {
      var allINPUTs = trs[i].getElementsByTagName("INPUT");
      var rowsum = 0;
      for (var j=0; j<allINPUTs.length; j++)
      {
      rowsum += parseInt(allINPUTs[j].value, 10);
      }
      trs[i].lastChild.firstChild.innerText = rowsum;
      }
      }
      </script>
      </head>
      <body>
      <form>
      <table>
      <tr>
      <td><Input type="text" name="col1_1"></td>
      <td><Input type="text" name="col2_1"></td>
      <td><Input type="text" name="col3_1"></td>
      <td><span></span></td>
      </tr>
      <tr>
      <td><Input type="text" name="col1_2"></td>
      <td><Input type="text" name="col2_2"></td>
      <td><Input type="text" name="col3_2"></td>
      <td><span></span></td>
      </tr>
      </table>
      <input type="button" value="go" onclick="go()" />
      </form>
      </body>
      </html>

      说明:
      0。如果这种方案能满足你的需求,则 input 不需要 id
      1。没有写 error handling
      2。如果不想要 button,可以考虑使用 expression....更多精彩文章及讨论,请光临枫下论坛 rolia.net
      • 1. 有可能Netscape不通过 2. 如果每行的TextInput之间夹杂个把CheckInput, HiddenInput也不行。
        • 1. 坚持使用 w3c dom 即可。 2. 随便加个 custom attribute 就可以乐。
          • 请教,第一个能否详细说说?因为我印象中这段代码在NS4估计不通过。
            • nn4? 可能。我顶多支持到 nn6.2,再烂的实在没办法支持。;-) 偶脚得只用 w3c dom 已经很不爽了,再要支持 nn4 ----
      • Did you test the code? doesn't work. Thank you anyway
        • of cuz i did test it... it works on IE6.... and i believe it should work w/ ie 55. and i know it won't work w/ NN, but if u modify innerText, it should work w/ nn6.2+