[英]remove a row from dynamically created table using jquery
在我的表單中,im借助以下模板創建動態表。
<table id="Newservice" style="display:none">
<tr>
<td><input id="type-%" style="width:100px" class="act_type" type="text" name="provider_service_dtls[#].activity_type" readonly value /></td>
<td><input id="code-%" class="act_code" style="width:150px" type="text" name="provider_service_dtls[#].activity_code" value /></td>
<td><input id="start-%" class="datepicker" style="width:125px" type="text" name="provider_service_dtls[#].activity_start_date" value /></td>
<td><input id="clinician-%" class="clini" style="width:200px" type="text" name="provider_service_dtls[#].clinician" value /></td>
<td><input id="net-%" class="" style="width:40px" type="text" name="provider_service_dtls[#].net_amt" value /></td>
<td><input id="qty-%" class="" style="width:40px" type="text" name="provider_service_dtls[#].quantity" value />
<input type="hidden" name="provider_service_dtls.Index" value="%" />
</td>
<td><input id="delete" class="delete" value="X" type="button"></td>
</tr>
</table>
單擊“新建”按鈕時,它正在為“服務”表成功創建新行。
現在,當單擊“ x”按鈕時,我需要將其刪除。
我寫了如下的jQuery
$('#service').on('click', buttonSelector, function () {
$(this).closest('tr').remove();
});
但是它沒有用..任何人以前都做過。 請幫忙
嘗試更新到:
$('#service').on('click', 'td input.delete', function () {
$(this).closest('tr').remove();
});
希望這個能對您有所幫助
$(document).ready(function () {
$(".delete").bind("click", function() {
$(this).closest('tr').remove();
});
});
或這個
$(document).on('click', ".delete", function () {
$(this).closest('tr').remove();
});
嘗試這樣,由於tr
是動態的,因此必須使用事件委托:
$(document).on('click', "input.delete", function () {
$(this).closest('tr').remove();
});
你很親密
根據您的代碼,您可以簡單地將$('#service').on()
替換為:
$('#delete').on('click', function () {
$(this).closest('tr').remove();
});
您必須為所有刪除按鈕注冊click事件,但僅為先前選擇的buttonSelector
注冊click事件。 所以你應該使用'td input.delete'
選擇器而不是buttonSelector
;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.