【发布时间】:2018-07-06 03:06:19
【问题描述】:
我想删除动态创建的表行。单击Add New 时会创建新的表格行。我只能删除已经加载的表格行。
请帮我指出代码的问题。
.html
<table id="fresh-table" class="table table-striped option-list table-hover table-sm">
<thead class="thead-table-list">
<tr>
<th scope="col">Option</th>
<th scope="col">Answer</th>
<th></th>
</tr>
</thead>
<tboy>
<tr>
<td><input type="text" class="form-control" value="Animal"></td>
<td><input type="text" class="form-control" value="F"></td>
<td><i class="material-icons option-delete">delete</i></td>
</tr>
<tr>
<td><input type="text" class="form-control" value="Snake"></td>
<td><input type="text" class="form-control" value="T"></td>
<td><i class="material-icons option-delete">delete</i></td>
</tr>
<tr>
<td><input type="text" class="form-control" value="Eagle"></td>
<td><input type="text" class="form-control" value="F"></td>
<td><i class="material-icons option-delete">delete</i></td>
</tr>
<tr>
<td><input type="text" class="form-control" value="Turtle"></td>
<td><input type="text" class="form-control" value="F"></td>
<td><i class="material-icons option-delete">delete</i></td>
</tr>
<tr>
<td colspan="3"><u id="add_option">Add New</u></td>
</tr>
</tboy>
</table>
.js
<script type='text/javascript'>
//Add option row-- QCM
$("#add_option").click(function(){
$('.option-list tr:nth-last-child(2)').after('<tr><td><input type="text" class="form-control" value="Turtle"></td><td><input type="text" class="form-control" value="F"></td><td><i class="material-icons option-delete">delete</i></td></tr>');
});
//Delete option_row onClick
$('td i').on('click',function(e){
e.preventDefault();
$(this).parents('tr').remove();
});
</script>
代码有什么问题吗?
【问题讨论】:
标签: jquery django jquery-selectors