【发布时间】:2018-08-18 21:31:48
【问题描述】:
我有一个表格,顶部有加号或减号字形图标,允许用户添加或减去行。我的代码有效,但如果用户点击减号按钮太多次,它会吃掉整个桌子。
我尝试的是在我添加的行中添加一个唯一的 ID 标签,并且只删除带有 ID 的 TR,但是如果添加了 2 行并按下减号,这两行将同时被删除,我只想一次删除一行。注意:下面的代码不反映这种尝试。
字形:
<div class="well" style="margin-bottom:0px;" id="addrow">
<span class="glyphicon glyphicon-plus" id="add"></span>
<span class="glyphicon glyphicon-minus" id="minus"></span>
表:
<table class="table table-bordered table-striped" style="margin-bottom:0px;" id="myTable">
<tr>
<td>Customer Master #</td>
</tr>
<tr>
<td><input type="text" class="form-control" ></td>
</tr>
</table>
JS:
$(document).ready(function(){
$("#addrow").on("click", "span#add", function(){
var tablerow = '<tr><td><input type="text" class="form-control" ></td></tr>'
$("#myTable tr:last").after(tablerow);
})
$("#addrow").on("click", "span#minus", function(){
$('#myTable tr:last').remove();
} )
});
【问题讨论】:
-
使用类而不是 ID,ID 应该是唯一的
-
你是对的,但无论我使用哪个,都会发生相同的结果。
-
我无法重现您的错误...请用有问题的代码更新小提琴jsfiddle.net/GRUFA
-
This 可能会有所帮助。
标签: javascript jquery html-table