将值从数组传递到文本框时出错

时间:2013-07-03 04:39:44

标签: javascript

我试图根据逗号分隔的分隔符拆分列表数组值。然后将其放入具有5个逗号分隔值的数组中。然后我再次在逗号分隔值上分隔每个数组并将它们分配给文本框。 但我收到以下错误。请帮忙。

  

错误:对象不支持此属性或方法。

<body>
   <script>
      function functionOne()  {
         var list = "[768.234.232, 768.234.232, 574, 2013-06-29, 2013-06-29, 768.234.232, 768.234.232, 987, 2013-06-29, 2013-06-29]";
         list = list.slice(1,-1);                 // remove the enclosing []
         var allValues = list.split(/\s*,\s*/);   // split on comma with optional whitespace
         var a = [];
         var counter=0;
         for ( var i = 0 ; i < allValues.length ; i += 5 ) {
             a.push( allValues.slice( i, i+5 ) );

             for (var k = 0 ; k < a.length ; k++ ) {
                foo = a[i].split(",");
                for (var j = 0 ; j < foo.length ; j++ ) {
                   document.getElementById('name' + counter).value = foo[j];
                   counter++;
                }
             }
          }
      }

    </script>

    <form>
        <table class="cmn-table" id="t1" border="1"
            style="margin-left: 0.2em; margin-right: 0em">
        <tr>
            <th>Sl.No</th>
            <th>Source IP Address</th>
            <th>Destination IP Address</th>
            <th>Service and Port (TCP, UDP, etc.)</th>
            <th>Start Date</th>
            <th>Expiration Date</th>
        </tr>
        <tr>
            <td><input type="text" name="name11" id="s1" value="1" size="2"
                    readonly="readonly" /></td>
            <td><input type="text" name="name0" id="name0" size="20"></td>
            <td><input type="text" name="name1" id="name1" size="20"></td>
            <td><input type="text" name="name2" id="name2" size="20"></td>
            <td><input type="text" name="name3" id="name3" size="15"></td>
            <td><input type="text" name="name4" id="name4" size="15"></td>
        </tr>
        <tr>
            <td><input type="text" name="name" id="s2" size="2" value="2"
                    readonly="readonly" /></td>
            <td><input type="text" name="name0" id="name5" size="20"></td>
            <td><input type="text" name="name1" id="name6" size="20"></td>
            <td><input type="text" name="name2" id="name7" size="20"></td>
            <td><input type="text" name="name3" id="name8" size="15"></td>
            <td><input type="text" name="name4" id="name9" size="15"></td>
        </tr>
        </table>

    </form>
    <input type="button" value="Click" onclick="functionOne()">

</body>

2 个答案:

答案 0 :(得分:3)

这里有一个问题:

      for (var k = 0; k < a.length; k++) {
            foo = a[i].split(",");

你的意思是

      for (var k = 0; k < a.length; k++) {
            foo = a[k].split(",");
                    ^ THIS

答案 1 :(得分:1)

这是你的代码中的错误/错误:从(foo = a [i] .split(“,”);)更改为foo = a [k]; 我让你成了小提琴:http://jsfiddle.net/Qtsqp/

  for (var i = 0; i < allValues.length; i+=5) {
      a.push( allValues.slice(i, i+5) );

      for (var k = 0; k < a.length; k++) {

            foo = a[k];
            for (var j = 0; j < foo.length; j++) {
                document.getElementById('name' + counter).value = foo[j];
                counter++;
            }
        }
  }