迭代java脚本中的不同文本框并分配值

时间:2012-12-17 18:18:01

标签: javascript

我试图循环遍历文本框并为其赋值。我创建了三个数组列表ids[]id1s[]id2s[]

ids[]中的值必须填入第一列,并且即将到期。但是id1s[]id2[]中的值需要填入第2和第3列。循环运行5次。第一次填充第一行,但是从下一次开始,它会覆盖第一行......而不会进入第二行

以下是我的代码: -

<!DOCTYPE html>
  <html>
    <head>
      <script>
        function start()
        {
          var ids = ["A", "B", "C", "D", "E"];
          var id1s = [1,2,3,4,5];
          var id2s = [6,7,8,9,10];
          for (var i = 0; i < ids.length; i++){
            var value=ids[i];
            addrow(value);
            addData(id1s[i], id2s[i]);
          }
        }

        function addrow(value)
        {
          var test1 = value+'1';
          var test2 = value+'2';
          var TABLE = document.getElementById('tableId');
          var BODY = TABLE.getElementsByTagName('tbody')[0];
          var TR = document.createElement('tr');
          var TD1 = document.createElement('td');
          var TD2 = document.createElement('td');
          var TD3 = document.createElement('td');
          TD1.innerHTML = value;
          TD2.innerHTML = "<input type='text' id='test1' value=''>";
          TD3.innerHTML = "<input type='text' id='test2' value=''>";
          TR.appendChild (TD1);
          TR.appendChild (TD2);
          TR.appendChild (TD3);
          BODY.appendChild(TR);
        }

        function addData(num,num1)
        {
          alert("Showing assignment of values");
          document.getElementById("test1").value=num;
          document.getElementById("test2").value=num1;
        }
      </script>
    </head>
  <body>

  <table id="tableId" border='1' cellspacing='0' cellpadding='0'>
  <tr>
    <td>Variable</td> 
    <td>Value1</td>
    <td>Value2</td>
  </tr>
  <button type="button" onclick="start()">Display</button>                  
  </table>

</body>

请建议如何遍历在第2和第3列中创建的文本框

2 个答案:

答案 0 :(得分:1)

我修改了你的addrow和addData函数

用于addrow功能 您必须使用test1和test2中的值来形成输入ID 像这样

TD2.innerHTML ="<input type='text' id='"+ test1 + "' value=''>";
TD3.innerHTML ="<input type='text' id='"+ test2 + "' value=''>";
对于addData函数,

将'value'变量作为参数引入 并将document.getElementById语句更改为

document.getElementById(value + "1").value=num;
document.getElementById(value + "2").value=num1;

请检查jsFiddle http://jsfiddle.net/TbGcD/1/

答案 1 :(得分:0)

根据我的经验,这样的事情应该提供不错的表现

<!DOCTYPE html>
    <html>
        <head>
        <script>
        function start()
        {
            var ids = ["A", "B", "C", "D", "E"];
            var id1s = [1,2,3,4,5];
            var id2s = [6,7,8,9,10];
            var table= document.getElementById('tableId').getElementsByTagName('tbody')[0];

            for (var i = 0; i < ids.length; i++){
                alert("Showing assignment of values");
                table.innerHTML+= '<tr><td>'+ids[i]+'</td><td>'+id1s[i]+'</td><td>'+id2s[i]+'</td></tr>';
            }
        }
        </script>
    </head>
    <body>
    <button type="button" onclick="start()">Display</button>
    <table id="tableId" border='1' cellspacing='0' cellpadding='0'>
        <tr>
            <td>Variable</td> 
            <td>Value1</td>
            <td>Value2</td>
        </tr>
    </table>
</body>

如果您不需要单独分配每一行,我建议您这样做

<!DOCTYPE html>
    <html>
        <head>
        <script>
        function start()
        {
            var ids = ["A", "B", "C", "D", "E"];
            var id1s = [1,2,3,4,5];
            var id2s = [6,7,8,9,10];
            var table= document.getElementById('tableId').getElementsByTagName('tbody')[0];
            var text='';

            for (var i = 0; i < ids.length; i++){
                text+='<tr><td>'+ids[i]+'</td><td>'+id1s[i]+'</td><td>'+id2s[i]+'</td></tr>';
            }
            table.innerHTML+= text;
            text='';
        }
        </script>
    </head>
    <body>
    <button type="button" onclick="start()">Display</button>
    <table id="tableId" border='1' cellspacing='0' cellpadding='0'>
        <tr>
            <td>Variable</td> 
            <td>Value1</td>
            <td>Value2</td>
        </tr>
    </table>
</body>