我有一个Bootstrap表,如下所示,
<table class="table table-striped table-bordered">
<tr><th>First Name</th><th>Last Name</th><th>Age</th><th>Delete</th></tr>
<tr><td>Mickey</td><td>Mouse</td><td>5</td><td><button type="submit" class="btn btn-default btn-sm"><span class="glyphicon glyphicon-trash"></span></button></td>
<tr><td>Tom</td><td>Cat</td><td>6</td><td><button type="submit" class="btn btn-default btn-sm"><span class="glyphicon glyphicon-trash"></span></button></td>
<tr><td>Pooh</td><td>Bear</td><td>4</td><td><button type="submit" class="btn btn-default btn-sm"><span class="glyphicon glyphicon-trash"></span></button></td>
<tr><td>Donaled</td><td>Duck</td><td>7</td><td><button type="submit" class="btn btn-default btn-sm"><span class="glyphicon glyphicon-trash"></span></button></td>
<tr><td>Jerry</td><td>Mouse</td><td>8</td><td><button type="submit" class="btn btn-default btn-sm"><span class="glyphicon glyphicon-trash"></span></button></td>
</table
每当用户点击垃圾按钮时,该特定行都将被删除。如何使用jQuery实现这一目标?
这是我到目前为止的链接 - http://jsbin.com/gabodamace/edit?html,output
提前致谢!
答案 0 :(得分:3)
答案 1 :(得分:3)
$('table').on('click','.delete',function(){
$(this).parents('tr').remove();
});
在您的代码中添加class="btn btn-default btn-sm delete
。添加类delete
也可以帮助您在其他处理程序中使用btn
类。 .remove()
函数删除该选择器的整个html。
请参阅jsFiddle:https://jsfiddle.net/jagrati16/o7bu09jr/1/