jquery - 通过jquery添加的css id不起作用
问题描述
我通过 jquery 动态地在 html 中添加表。我可以补充。但我无法删除添加的表格。从 jquery 附加的 CSS 'remove' id 不起作用。'remove' Id 被添加到脚本最后的部分标签中
jQuery
$().ready(function(){
$('#AddMoreId').on('click', function(){
$('article:last').append('<article class="col100 left bggreen"><table><tr><th>ID</th><td><input type="number" id="id"></td></tr><tr><th>Menu in NPL</th><td><input type="text" id="menunep"></input></td></tr><tr><th>Menu in ENG</th><td><input type="text" id="menueng"></input></td></tr></table><section class="col100 left cursorpointer" id="remove">Remove</section></article><br/>');
});
$('#remove').on('click', function(){
$(this).parents('article').remove();
});
});
html
<article class="col100 left bggreen">
<table>
<tr>
<th>
ID
</th>
<td>
<input type="number" id="id">
</td>
</tr>
<tr>
<th>
Menu in NPL
</th>
<td>
<input type="text" id="menunep"></input>
</td>
</tr>
<tr>
<th>
Menu in ENG
</th>
<td>
<input type="text" id="menueng"></input>
</td>
</tr>
<tr>
<td class="cursorpointer" colspan="2" id="AddMoreId">Add field
</td>
</tr>
</table>
</article>
解决方案
对于动态添加的项目,请使用以下内容:
$(document).on('click', '#remove', function(){
$(this).parents('article').remove();
});
推荐阅读
- linux - 如何使用 sed 从长字符串中提取键的值?
- haskell - 新数据的模式匹配
- java - 将 POJO 序列化为 JSON - 输入参数化
- jquery - jQuery 动态搜索字段 stopPropagation 无法正常工作
- c++ - CMake 测试在 MacOS 上找不到库
- python - Python:调用具有给定参数列表的方法
- youtube-api - API 密钥不再工作,但不知道它与哪个帐户对齐
- firefox - 带有 FIrefox 62 的网页中未出现 Google 表单
- google-cloud-firestore - Firestore 中不同集合中相互依赖的文档的正确模式是什么?
- sql - SQL Server 'LIKE' 或 varchar 问题