[英]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.