如何删除添加的行而不删除表标题

时间:2017-07-17 08:05:30

标签: javascript jquery

现在,我有一个重置按钮来重置已添加的所有行。问题是它还删除了我想保留的表标题。 保持表格的任何想法?谢谢你们。

$('#add-row').click(function() {
  var $tbody, $row, additionalRows;
  var numNewRows = parseInt($('#insert-rows-amnt').val(), 10);

  if (isNaN(numNewRows) || numNewRows <= 0) {
    alert('Please enter number of injection');
  } else {

    $tbody = $('table#one tbody ');
    $row = $tbody.find('tr:last');
    var lastRowIndex = ($row.index() == -1 ? 0 : $row.index()) + 1;
    additionalRows = new Array(numNewRows);
    for (i = 0; i < numNewRows; i++) {
      additionalRows[i] = ` <tr>
    <td>${lastRowIndex}</td>
      <td>
      <input type="text" style="width: 100px" name="vaccineid[]"></td>
      <td><input type="text" style="width:160px"name="vaccinename1[]">   				</td>
  	  </tr>`
      lastRowIndex = lastRowIndex + 1;
    }

    $tbody.append(additionalRows.join());
  }
});

$('[name="reset"]').click(function() {
  $('#one tr').remove();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<input type="number" id="insert-rows-amnt" name="insert-rows-amnt" value="<?php echo $tam ?>" />
<button id="add-row" type="button">Add Row</button>
<button type="reset" name="reset">Reset Row</button>

<table id="one">
  <thead>
    <th>No.</th>
    <th>Name</th>
    <th>Gender</th>
  </thead>
  <tbody>
  </tbody>
</table>

1 个答案:

答案 0 :(得分:2)

只需选择<TBODY>

即可
$('#one tbody tr').remove();