简体   繁体   English

动态删除复选框的问题。

[英]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.当我想删除时,它似乎不起作用。

Live Demo现场演示

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

        });

DEMO演示

Try below jQuery :试试下面的jQuery

$('#cblist').on('click','.removeCheckBoxDialog', function (e) {            
    $('#'+$(this).prev().attr('for')).remove();
    $(this).next('br').remove().prev().addBack().remove();
    $(this).remove();
}); 

Fiddle小提琴


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();
}); 

Fiddle小提琴

Try this尝试这个

 $("#cb"+id).remove();
 $('label[for=cb'+id+']').remove();

Try this .试试这个 I have done the following:我做了以下工作:

  1. Appended a div to contain each of the 3 components附加一个 div 以包含 3 个组件中的每一个
  2. I used var container2 = $("div:last-of-type", container);我使用var container2 = $("div:last-of-type", container); to select this div.选择这个div。
  3. Finally I simply used $(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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM