英文:
How to delete dynamically added html code block
问题
I have a button which dynamically creates html content enclosed in <fieldset>
like this.:
<div id="box_parent"></div>
Javascript
var content =
'<fieldset>Some Content <input id="delete_row_box" type="button" value="Delete"></fieldset>';
$("#box_parent").append(content);
How can I make the dynamically created delete button only delete the HTML block that it belongs to?
I'm stuck on this function:
$(document).on("click", '#delete_row_box', function() {
console.log(this);
});
I think .remove()
should do it but I don't know how to throw the correct reference.
英文:
I have a button which dynamically creates html content enclosed in <fieldset>
like this.:
<div id="box_parent"></div>
Javascript
var content =
'<fieldset>Some Content <input id="delete_row_box" type="button" value="Delete"></fieldset>';
$("#box_parent").append(content);
How can I make the dynamically created delete button only deletes the html block that it only belongs to?
I'm stuck on this function:
$(document).on("click", '#delete_row_box', function() {
console.log(this);
});
I think .remove()
should do it but I don't know how to throw the correct reference
答案1
得分: 2
Sure, here is the translated content:
Whole Content
如果您想要使用jQuery删除父容器中的整个内容,请使用以下代码:
$(document).on("click", '#delete_row_box', function() {
$("#box_parent").children().remove();
});
One Row
不要为可重复标签使用ID。在JS中无法正常工作。只需像我在示例中使用的适当类名。
如果您想要附加特定的框以进行删除操作,那么您应该在附加时为子项指定ID,或者应该将删除按钮放置在每个框内,类似于每个项目的关闭按钮。
如果行的容器是按钮的直接父级
使用以下代码查找要移除的父级:
$(document).on("click", '.delete_row_box', function() {
$(this).parent().remove();
});
如果行的容器是按钮的爷爷级
使用以下代码查找要移除的行:
$(document).on("click", '.delete_row_box', function() {
$(this).closest('fieldset').remove();
});
- 您可以将
fieldset
替换为与您的父级匹配的选择器。
英文:
Whole Content
If you want to delete the whole content in box parent using jQuery, you should use this code:
$(document).on("click", '#delete_row_box', function() {
$("#box_parent").children().remove();
});
One Row
Do not use ID for repeatable tags. It's not working in JS. Just use a suitable class name as I used in examples
If you want a specific box appended to delete, then you should either give an id to the child during append or you should place the delete button inside the each box like a close button for each item.
If row's container is direct parent of the button
Find the parent to remove using this code:
$(document).on("click", '.delete_row_box', function() {
$(this).parent().remove();
});
If row's container is a grand parent of the button
Find the row you need to remove using this code:
$(document).on("click", '.delete_row_box', function() {
$(this).closest('fieldset').remove();
});
- you can replace
fieldset
with the selector that matches your parent.
答案2
得分: 0
选择其父级,然后删除您添加的 fieldset
。
$(this).parent().remove()
英文:
Select its parent then delete the fieldset
you add
$(this).parent().remove()
答案3
得分: 0
尝试添加一个id并移除那个id。
这段代码应该能正常工作,它只会删除"一些内容",不会移除你创建的删除按钮。如果你需要生成多个内容,你可以在创建内容时添加一个索引,然后使用for循环或while循环来选择要删除的内容(带有多个删除按钮)。你甚至可以使用类而不是id来一次性删除它们所有。
var content = '<fieldset><span id="content">一些内容</span> <input id="delete_row_box" type="button" value="删除"></fieldset>';
$("#box_parent").append(content);
$(document).on("click", '#delete_row_box', function() {
$("#content").remove();
});
英文:
try to add an id and remove that id.
This code should work fine and it will only delete "Some content", it will not remove the delete button you created. If you need to generate multiple content you can add an index when you create the content and a for or while loop to select the content to remove (with multiple delete button). You can even use a class instead of an id to delete them all in one go.
var content = '<fieldset><span id="content">Some Content</span> <input
id="delete_row_box" type="button" value="Delete"></fieldset>';
$("#box_parent").append(content);
$(document).on("click", '#delete_row_box', function() {
$("#content").remove();
});
答案4
得分: 0
<!-- 开始代码片段: js 隐藏: false 控制台: true Babel: false -->
<!-- 语言: lang-js -->
var content = ''<fieldset>一些内容 <input id="delete_row_box" type="button" value="删除"></fieldset>'';
$("#box_parent").append(content);
$(document).on("click", '#delete_row_box', function() {
$(this).closest('fieldset').remove();
});
<!-- 语言: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="box_parent"></div>
<!-- 结束代码片段 -->
英文:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
var content = '<fieldset>Some Content <input id="delete_row_box" type="button" value="Delete"></fieldset>';
$("#box_parent").append(content);
$(document).on("click", '#delete_row_box', function() {
$(this).closest('fieldset').remove();
});
<!-- language: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="box_parent"></div>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论