删除jQuery中的动态表行不起作用 [英] Remove Dynamic Table row in jQuery isn't working

查看:63
本文介绍了删除jQuery中的动态表行不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用javascript创建动态表,以便可以动态添加和删除元素. 现在,删除功能可能无法正常工作.

I'm trying to make dynamic table with javascript that elements can be dynamically added and removed with it. Now, the remove function doesn't work probably.

我已经尝试过.parent().parent().remove();

I've tried .parent().parent().remove();

.parents("tr").remove();

.parents("tr").remove();

.closest("tr").remove();

.closest("tr").remove();

但是它们不起作用.

要附加到表中的代码:

$("table tbody").append("<tr><input type='hidden' name='label[]' value='" + $(".inputTitle").val() + "' /><input type='hidden' name='htmlText[]' value=\"" + html + "\" /><td>" + html + "</td><td>" + $(".inputTitle").val() + "</td><td>" + typeName + " ( " + crArabic + " )</td><td><a class='btn btn-danger deleteRow'><i class='fa fa-fw fa-times' aria-hidden='true'></i></a></td></tr>");

删除代码:

$("table tbody tr").on("click", ".deleteRow", function () {
    $(this).parent().parent().remove();
});

此操作的预期结果是动态添加和删除表行.但是,删除事件无效.

The expected results to this is to add and remove table rows dynamically. However, the removing event isn't working.

推荐答案

您可能必须阅读此答案,然后将代码更改为:

You may have to read this SO answer Difference between $(document).on("click", "selector", function …); and $("selector").on("click", function …) and change your code to:

$(document).on("click", ".deleteRow", function () { $(this).parent().parent().remove(); });

这篇关于删除jQuery中的动态表行不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆