我有以下 jquery 代码来添加和删除表中的一行。我在一页上有多个表格。每个表中的每一行都有这两个类:.row-add 和 .row-delete
现在,当我单击“.row-add”添加新行时,所有表都会受到影响,这意味着该行将添加到同一页面上的所有表中。我应该怎么做才能使它在单击时仅适用于自己的表?
查询:
$(document).ready(function () {
$('.row-delete').click(function () {
$(this).closest("tr").remove();
});
$('.row-add').click(function () {
$(this).closest("tr").clone(true).appendTo(".table-comparison");
});
});
html:
<div id="tab-1" class="tab-section">
<div class="container flat rounded-sm bspace">
<table cellspacing="0" class="display table-comparison">
<thead>
<tr>
<th><span>Effective Date</span></th>
<th><span>Price</span></th>
</tr>
</thead>
<tbody>
<tr>
<td><input class="effective-date" type="text" value="01/01/2013"> - <input class="effective-date" type="text" value="06/05/2015">
<span class="row-add"></span>
<span class="row-delete"></span>
</td>
<td>
$<input class="price" value="50">
/
<select>
<option>Second</option>
<option>Minute</option>
<option>Hour</option>
<option>Day</option>
<option>Week</option>
<option>Biweek</option>
</select>
/
<select>
<option>Day</option>
<option>Week</option>
<option>Biweek</option>
<option>Month</option>
<option>Quarter</option>
<option>Year</option>
</select>
<span class="row-add"></span>
<span class="row-delete"></span>
</td>
</tr>
<tr class="price-present">
<td><input class="effective-date" type="text" value="07/01/2013"> - <span class="effective-date">Present</span>
<span class="row-add"></span>
<span class="row-delete"></span>
</td>
<td>
$<input class="price" value="40">
/
<select>
<option>Second</option>
<option>Minute</option>
<option>Hour</option>
<option>Day</option>
<option>Week</option>
<option>Biweek</option>
</select>
/
<select>
<option>Day</option>
<option>Week</option>
<option>Biweek</option>
<option>Month</option>
<option>Quarter</option>
<option>Year</option>
</select>
<span class="row-add"></span>
<span class="row-delete"></span>
</td>
</tr>
<tr>
<td><input class="effective-date" type="text" value="01/01/2011"> - <input class="effective-date" type="text" value="06/30/2012">
<span class="row-add"></span>
<span class="row-delete"></span>
</td>
<td>
$<input class="price" value="30">
/
<select>
<option>Second</option>
<option>Minute</option>
<option>Hour</option>
<option>Day</option>
<option>Week</option>
<option>Biweek</option>
</select>
/
<select>
<option>Day</option>
<option>Week</option>
<option>Biweek</option>
<option>Month</option>
<option>Quarter</option>
<option>Year</option>
</select>
<span class="row-add"></span>
<span class="row-delete"></span>
</td>
</tr>
</tbody>
</table>
</div>
</div>