[英]Problems with remove checkbox dynamically.
I have looked all over and found many similar threads, but none of them really answered my question with this specific situation:我查看了所有内容,发现了许多类似的主题,但没有一个真正回答了我在这种特定情况下的问题:
I want to, when I create a dynamic Checkbox, and want to remove the specific checkbox and the text by clicking on the trash image.我想,当我创建一个动态复选框时,并希望通过单击垃圾图像来删除特定的复选框和文本。 It seems to not work when I want to remove.
当我想删除时,它似乎不起作用。
HTML: HTML:
<input type="text" id="checkBoxName" />
<input type="button" value="ok" id="btnSaveCheckBox" />
Jquery:查询:
$(document).ready(function() {
$('#btnSaveCheckBox').click(function() {
addCheckbox($('#checkBoxName').val());
$('#checkBoxName').val("");
});
});
function addCheckbox(name) {
var container = $('#cblist');
var inputs = container.find('input');
var id = inputs.length+1;
$('<input />', { type: 'checkbox', id: 'cb'+id, value: name }).appendTo(container);
$('<label />', { 'for': 'cb'+id, text: name }).appendTo(container);
$('<img />', { "src": "/Pages/Images/trashDialog.png", "class": "removeCheckBoxDialog"}).appendTo(container);
$('.removeCheckBoxDialog').on('click', function (e) {
$("#cb :checkbox").remove();
});
$('<br/>').appendTo(container);
}
CSS: CSS:
.removeCheckBoxDialog {
margin-left:10%;
cursor:pointer;
}
Add this添加这个
$("#cb"+id).remove();
$('label[for=cb'+id+']').hide();
$(this).nextAll('br').remove();
$(this).remove();
to your click
function到您的
click
功能
$('.removeCheckBoxDialog').on('click', function (e) {
$("#cb"+id).remove();
$('label[for=cb'+id+']').remove();
$(this).nextAll('br').remove();
$(this).remove();
});
Try below jQuery :试试下面的jQuery :
$('#cblist').on('click','.removeCheckBoxDialog', function (e) {
$('#'+$(this).prev().attr('for')).remove();
$(this).next('br').remove().prev().addBack().remove();
$(this).remove();
});
The above wasnt clearing the label so i edited it a bit :上面没有清除标签所以我编辑了一下:
$('#cblist').on('click','.removeCheckBoxDialog', function (e) {
$('#'+$(this).prev().attr('for')).remove();
$(this).next('br').remove();
$(this).prev().remove();
$(this).remove();
});
Try this尝试这个
$("#cb"+id).remove();
$('label[for=cb'+id+']').remove();
Try this .试试这个。 I have done the following:
我做了以下工作:
var container2 = $("div:last-of-type", container);
var container2 = $("div:last-of-type", container);
to select this div.$(this).parent().remove();
$(this).parent().remove();
To get the parent and remove it. And also I removed the <br>
as the div does the job of new line by default.而且我还删除了
<br>
因为 div 默认执行新行的工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.