删除jQuery中的动态表行不起作用

时间:2019-01-12 16:22:14

标签: javascript jquery

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

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

.parents(“ tr”)。remove();

.closest(“ tr”)。remove();

但它们不起作用。

要附加到表的代码:

android { defaultConfig { testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunne" } testOptions { execution 'ANDROIDX_TEST_ORCHESTRATOR' } } dependencies { testImplementation 'junit:junit:4.12' androidTestImplementation 'androidx.test:runner:1.1.1' androidTestUtil 'androidx.test:orchestrator:1.1.1' androidTestImplementation 'androidx.test.ext:junit:1.1.0' androidTestImplementation 'androidx.test:rules:1.1.1' androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1' androidTestImplementation 'androidx.test.espresso:espresso-contrib:3.1.1' }

删除代码:

$("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>");

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

1 个答案:

答案 0 :(得分:1)

您可能必须阅读此答案Difference between $(document).on(“click”, “selector”, function …); and $(“selector”).on(“click”, function …),并将代码更改为:

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