如何在按钮上单击特定行表上的行添加行?

时间:2016-02-19 06:54:23

标签: javascript php

当我点击添加更多按钮时,我想在每一行上添加按钮,它应该在该行输入字段之后添加新行

var counter = 1;
function addInput(divName){
    var newdiv = document.createElement('div');
    newdiv.innerHTML = "Entry " + (counter + 1) + " <br><input type='text' name='myInputs[]'>";
    document.getElementById(divName).appendChild(newdiv);
    counter++;
}

2 个答案:

答案 0 :(得分:0)

您可以使用

&#13;
&#13;
function addRow(tableID) {
  var table = document.getElementById(tableID);
  var rowCount = table.rows.length;
  var row = table.insertRow(rowCount);
  
  var cell2 = row.insertCell(0);
  cell2.innerHTML = rowCount + 1;
  
  var cell3 = row.insertCell(1);
  var element2 = document.createElement("input");
  element2.type = "text";
  element2.name = "txtbox[]";
  cell3.appendChild(element2);
}
&#13;
<INPUT type="button" value="Add Row" onclick="addRow('dataTable')" />

<TABLE id="dataTable" width="350px" border="1">
  <TR>
    <TD> 1 </TD>
    <TD> <INPUT type="text" /> </TD>
  </TR>
</TABLE>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

You can use this:

<button type="button" id="add-row">Add Row</button>
<table>
    <tbody>
        <tr>
            <td>1</td>
            <td>name</td>
        </tr>
    </tbody>
</table>

<script>
var id = 2; 
var row = '<tr><td>'+id+'</td><td>name</td></tr>';
$('#add-row').click(function(){
    $('tbody').apppend(row);
    id++;
});
</script>