我有以下jQuery代碼來添加和刪除表中的一行。我在一個頁面上有多個表格。每個表中的每一行都有以下兩個類:.row-add和.row-delete添加/刪除表中的行
現在當我點擊'.row-add'添加一個新行時,所有表都會受到影響,這意味着行是添加到同一頁面上的所有表格中。我應該怎麼做才能使它只在點擊時適用於自己的桌子?
的Jquery:
$(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>
我在這裏複製的HTML也是如此。你可以重新寫jQuery的一部分? – wcdomy
我是否需要爲每個表提供一個ID? – wcdomy
如果使用綁定函數,則不需要:傳遞給綁定的匿名函數的參數是啓動事件,因此您可以使用e.target訪問它。你獲得的是啓動事件的行。從那裏你可以拿起桌子等等。添加了代碼,但我仍然需要解析表格比較部分。 –