[英]How to remove a dynamically added div?
This is my HTML 这是我的HTML
<div class="form-group">
<label>Categories</label>
<button type="button" onclick="createContainer(availableCategories,'','.categoriesWrapper','Categories')" class="btn btn-default">+</button>
<div class="categoriesWrapper">
</div>
</div>
Inside I'm adding more divs with 'child' class with this function: 在内部,我通过此功能在'child'类中添加了更多div:
function createContainer(datasource, selectedItem, wrapper, name) {
var maxFields = 10;
var $container = $('<div style="margin-top:5px;" class="child" />');
var $select = $("<select class='form-control littleSpace' name='" + name + "'/>");
var $button = $("<button type='button' class='delete btn btn-default ' '>-</button>");
if ($(wrapper).children().length < maxFields) {
for (var itemId in datasource) {
//check to see if the option is the selected one
var isItemSelected = selectedItem && selectedItem === itemId;
var $option = null;
//create each option
if (isItemSelected == true) {
$option = $('<option value="' + itemId + '" selected>' + datasource[itemId] + '</option>');
}
else {
$option = $("<option value='" + itemId + "'>" + datasource[itemId] + "</option>");
}
//append option to select
$select.append($option);
}
$container.append($select);
$container.append($button);
$(wrapper).append($container);
}
}; };
Everything works great until now, but when I try to delete one o those divs and div content...it won't work. 到目前为止,一切都很好,但是当我尝试删除那些div和div内容中的某项时...将无法工作。 This is what I've tried:
这是我尝试过的:
$('.categoriesWrapper').on('click', 'delete', function () {
$(this).parent.remove();
});
Please help me 请帮我
Pass class with .
与一起通过课程
.
to make it work .delete
inside click function as well as per @Rayon noticed change parent
to parent()
使它工作单击功能内的
.delete
以及@Rayon注意到将parent
级更改为parent()
$('.categoriesWrapper').on('click', '.delete', function () {
$(this).parent().remove();
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.