使用JavaScript插入新的输入标记行

时间:2015-12-04 14:57:30

标签: javascript html html-table row

我想向<td>元素追加一个新的5个输入标记行,它是<tr class="unit">的子元素,是<table id="myTable">元素的孙子元素。

在我的Javascript代码中,我能够控制记录5个输入标记,但无法将它们附加到<td>元素和其他父元素。

我的代码是否正确解决这个问题?

这里是我正在做的事情的链接http://codepen.io/johnnyginbound/pen/xZxZNo?editors=101

&#13;
&#13;
function addRow(){
  var parentTable = document.getElementById('myTable');
  var tableRow = document.getElementsByTagName('tr')[0].children;
  var unitTables = document.getElementsByClassName('unit-table')[0];
  var newInputType = document.createElement('input');
  newInputType.setAttribute('type', 'text');
  
  for(var i=0; i<unitTables.children.length; i++){
    var appendedInput = unitTables.children[i].appendChild(newInputType)
    parentTable.appendChild(appendedInput);
  }
}

document.getElementById('add_btn').onclick=addRow;
&#13;
<form>

  <div class="panel panel-default">
    <!-- Default panel contents -->
    <div class="panel-heading">Panel heading</div>
    <div class="panel-body">
      <p> ASME Email: </p>
      <input type="text" name="email">
      <br />
      <br />

    </div>

    <!-- Table -->
    <table id="myTable" class="table">
      <tr>
        <th> Technology </th>
        <th> Markets </th>
        <th> Enduring/Emerging </th>
        <th> ASME Relevency </th>
        <th> Comments </th>
      </tr>

      <tr class="unit-table">
        <td>
          <input type="text">
        </td>
        <td>
          <input type="text">
        </td>
        <td>
          <input type="text">
        </td>
        <td>
          <input type="text">
        </td>
        <td>
          <input type="text">
        </td>
      </tr>
      <tr class="unit-table">
        <td>
          <input type="text">
        </td>
        <td>
          <input type="text">
        </td>
        <td>
          <input type="text">
        </td>
        <td>
          <input type="text">
        </td>
        <td>
          <input type="text">
        </td>
      </tr>
      <tr class="unit-table">
        <td>
          <input type="text">
        </td>
        <td>
          <input type="text">
        </td>
        <td>
          <input type="text">
        </td>
        <td>
          <input type="text">
        </td>
        <td>
          <input type="text">
        </td>
      </tr>
    </table>
  </div>

  <button id="add_btn">Add new row</button>
  <input type="submit" name="submit" value="Submit">
</form>
&#13;
&#13;
&#13;

4 个答案:

答案 0 :(得分:1)

您的问题是在每个按钮事件中刷新Web的表单标记。您的JavaScript代码也存在一些问题。

所以你的代码应该是

  <div class="panel panel-default">
    <!-- Default panel contents -->
    <div class="panel-heading">Panel heading</div>
    <div class="panel-body">
      <p> ASME Email: </p>
      <input type="text" name="email">
      <br />
      <br />

    </div>

    <!-- Table -->
    <table id="myTable" class="table">
      <tr>
        <th> Technology </th>
        <th> Markets </th>
        <th> Enduring/Emerging </th>
        <th> ASME Relevency </th>
        <th> Comments </th>
      </tr>

      <tr class="unit-table">
        <td>
          <input type="text">
        </td>
        <td>
          <input type="text">
        </td>
        <td>
          <input type="text">
        </td>
        <td>
          <input type="text">
        </td>
        <td>
          <input type="text">
        </td>
      </tr>
      <tr class="unit-table">
        <td>
          <input type="text">
        </td>
        <td>
          <input type="text">
        </td>
        <td>
          <input type="text">
        </td>
        <td>
          <input type="text">
        </td>
        <td>
          <input type="text">
        </td>
      </tr>
      <tr class="unit-table">
        <td>
          <input type="text">
        </td>
        <td>
          <input type="text">
        </td>
        <td>
          <input type="text">
        </td>
        <td>
          <input type="text">
        </td>
        <td>
          <input type="text">
        </td>
      </tr>
    </table>
  </div>

的JavaScript

function addRow(){
  var parentTable = document.getElementById('myTable');
  var myTd,myInput;
  var myTr = document.createElement('tr');
  myTr.setAttribute('class','unit-table');
  for (var i=0; i<5;i++){
    myTd = document.createElement('td');
    myInput = document.createElement('input');
    myInput.setAttribute('type','text');
    myTd.appendChild(myInput);
    myTr.appendChild(myTd);
  }
  parentTable.appendChild(myTr);
}
document.getElementById('add_btn').onclick=addRow;

答案 1 :(得分:0)

像这样

function addRow(){


            var table = document.getElementById("myTable");
            var rows = document.getElementById("myTable").rows.length;
            // add row 
                var row = table.insertRow(rows);

            // add input in cell 
            for(var i = 0; i < 5; i++){
              var cell1 = row.insertCell(i);
              var inputItem = document.createElement('input');
              cell1.appendChild(inputItem);
            }  

            }

答案 2 :(得分:0)

你的javascript和html应该如下

&#13;
&#13;
function addRow(){
        var table = document.getElementById("myTable");
        var rows = document.getElementById("myTable").rows.length;




        var table = document.getElementById("myTable");
        var rows = document.getElementById("myTable").rows.length;
        // add row 
            var row = table.insertRow(rows);

        // add input in cell 
        for(var i = 0; i < 5; i++){
          var cell1 = row.insertCell(i);
          var inputItem = document.createElement('input');
          cell1.appendChild(inputItem);
        }  

        }



document.getElementById('add_btn').onclick=addRow;
&#13;
<form>

  <div class="panel panel-default">
    <!-- Default panel contents -->
    <div class="panel-heading">Panel heading</div>
    <div class="panel-body">
      <p> ASME Email: </p>
      <input type="text" name="email">
      <br />
      <br />

    </div>

    <!-- Table -->
    <table id="myTable" class="table">
      <tr>
        <th> Technology </th>
        <th> Markets </th>
        <th> Enduring/Emerging </th>
        <th> ASME Relevency </th>
        <th> Comments </th>
      </tr>

      <tr class="unit-table">
        <td>
          <input type="text">
        </td>
        <td>
          <input type="text">
        </td>
        <td>
          <input type="text">
        </td>
        <td>
          <input type="text">
        </td>
        <td>
          <input type="text">
        </td>
      </tr>
      <tr class="unit-table">
        <td>
          <input type="text">
        </td>
        <td>
          <input type="text">
        </td>
        <td>
          <input type="text">
        </td>
        <td>
          <input type="text">
        </td>
        <td>
          <input type="text">
        </td>
      </tr>
      <tr class="unit-table">
        <td>
          <input type="text">
        </td>
        <td>
          <input type="text">
        </td>
        <td>
          <input type="text">
        </td>
        <td>
          <input type="text">
        </td>
        <td>
          <input type="text">
        </td>
      </tr>
    </table>
  </div>

  <input type="submit" name="submit" value="Submit">
</form>
  <button id="add_btn">Add new row</button>
&#13;
&#13;
&#13;

答案 3 :(得分:0)

这需要做很多工作。这是我的答案:)

https://jsfiddle.net/www139/1jwf02p7/

function addRow() {
  var table = document.getElementById('myTable');
  var columnLength = table.getElementsByTagName('tr')[0].children.length;
  var units = document.getElementsByClassName('unit-table');
  var tr = document.createElement('tr');
  tr.className = 'unit-table';
  for (var i = 0; i < columnLength; i++) {
    var td = document.createElement('td');
    var text = document.createElement('input');
    text.type = 'text';
    td.appendChild(text);
    tr.appendChild(td);
  }
  table.appendChild(tr);
}

document.getElementById('add_btn').onclick = addRow;
<!--<form>-->

<div class="panel panel-default">
  <!-- Default panel contents -->
  <div class="panel-heading">Panel heading</div>
  <div class="panel-body">
    <p>ASME Email:</p>
    <input type="text" name="email">
    <br />
    <br />

  </div>

  <!-- Table -->
  <table id="myTable" class="table">
    <tr>
      <th>Technology</th>
      <th>Markets</th>
      <th>Enduring/Emerging</th>
      <th>ASME Relevency</th>
      <th>Comments</th>
    </tr>

    <tr class="unit-table">
      <td>
        <input type="text">
      </td>
      <td>
        <input type="text">
      </td>
      <td>
        <input type="text">
      </td>
      <td>
        <input type="text">
      </td>
      <td>
        <input type="text">
      </td>
    </tr>
    <tr class="unit-table">
      <td>
        <input type="text">
      </td>
      <td>
        <input type="text">
      </td>
      <td>
        <input type="text">
      </td>
      <td>
        <input type="text">
      </td>
      <td>
        <input type="text">
      </td>
    </tr>
    <tr class="unit-table">
      <td>
        <input type="text">
      </td>
      <td>
        <input type="text">
      </td>
      <td>
        <input type="text">
      </td>
      <td>
        <input type="text">
      </td>
      <td>
        <input type="text">
      </td>
    </tr>
  </table>
</div>

<button id="add_btn">Add new row</button>
<input type="submit" name="submit" value="Submit">
<!--</form>-->