2017-07-17 47 views
0

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

$('#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[]"> \t \t \t \t </td> 
 
    \t </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>

+0

哦,对了,谢谢你了! – epiphany

回答

2

只需选择<TBODY>

$('#one tbody tr').remove(); 
+0

谢谢你的帮助彼得! – epiphany

+0

Btw Stackoverflow不会允许我现在接受你的答案,必须等待7分钟大声笑 – epiphany