简体   繁体   English

取消选中带有关闭按钮的复选框

[英]Uncheck checkbox with close button

Please, advice. 请指教。 I want to add tags inside the container marking checkboxes and remove tags by clicking on the "x" button of each tag. 我想在容器标记复选框内添加标签,并通过单击每个标签的“ x”按钮来删除标签。 Everything works fine except that when you click on the "x" button and tags are removed the state of the checkbox remains checked. 一切正常,除了单击“ x”按钮并删除标记后,复选框的状态保持选中状态。 But I need that when you click on the button of each tag the state of the checkbox must be unchecked. 但是我需要,当您单击每个标签的按钮时,必须取消选中复选框的状态。

It might be easier to do with the value and the name of input, but I can't use them - only id. 使用输入的值和名称可能更容易,但是我不能使用它们-仅ID。

$(document).ready(function() {
        var $list = $("#itemList");

    $(".chkbox").change(function() {
        var a = $('label[for="' + this.id + '"]').text();
            if (this.checked) {
        $list.append('<li><a href="#">'+a+'</a><button class="closebutton" value="'+a+'">X</button></li>');
        }
        else {
            $("#itemList li:contains('"+a+"')").remove();
        }
    })
    $(document).on('click','.closebutton',function(){
       var b = this.value;
       $("#itemList li:contains('"+b+"')").remove();
        $('label[for="' + this.id + '"]').removeAttr('checked');        
        });
    });

    <div id="items">
        <ul id="itemList">
        </ul>
    </div>
    <div id="tab1" class="tab-pane">
    <div id="ck-button"><label for="one"><input type="checkbox" id="one" class="chkbox"><span>First book</span></label> </div>
    <div id="ck-button"><label for="two"><input type="checkbox" class="chkbox" id="two"><span>  Second book</span></label> </div>
    </div>

Try 尝试

$(document).ready(function () {
    var $list = $("#itemList");

    $(".chkbox").change(function () {
        var a = $(this).next().text();
        if (this.checked) {
            $('<li><a href="#">' + a + '</a><button class="closebutton" value="' + a + '">X</button></li>').appendTo($list).data('src', this);

        } else {
            $("#itemList li:contains('" + a + "')").remove();
        }
    })
    $(document).on('click', '.closebutton', function () {
        var $li = $(this).closest('li');
        $($li.data('src')).prop('checked', false);
        $li.remove();
    });
});

Demo: Fiddle 演示: 小提琴

Try this: 尝试这个:

<div id="items">
    <ul id="itemList">
    </ul>
</div>
<div id="tab1" class="tab-pane">
    <div id="ck-button1"><label for="one"><input type="checkbox" id="one" class="chkbox"><span>First book</span></label>
    </div>
    <div id="ck-button2">
        <label for="two"><input type="checkbox" class="chkbox" id="two"><span> Second book</span></label></div>
</div>
</body>
<script>
    $(document).ready(function()
    {
        var $list = $("#itemList");
        $(".chkbox").change(function()
        {
            var a = $('label[for="'+this.id+'"]').text();
            var t = this.id;
            if(this.checked)
            {
                $list.append('<li><a href="#">'+a+'</a><button class="closebutton" title="'+t+'" value="'+a+'">X</button></li>');
            }
            else
            {
                $("#itemList li:contains('"+a+"')").remove();
            }
        })
        $(document).on('click', '.closebutton', function()
        {
            var b = this.value;
            var q = this;
            $("#itemList li:contains('"+b+"')").remove();
            $('#'+this.title).removeAttr('checked');
        });
    });
</script>

Or See this demo: http://jsfiddle.net/knYvf/ 观看此演示: http : //jsfiddle.net/knYvf/

This line should do the trick: 这行应该可以解决问题:

    $('label:contains("' + b + '")').find('.chkbox').prop('checked', false);

+1 to you for neat question as well! 也向您+1提出整洁的问题!

Hope any will suffice the need :) 希望任何人都能满足需要:)

Code

$(document).ready(function () {
    var $list = $("#itemList");

    $(".chkbox").change(function () {
        var a = $('label[for="' + this.id + '"]').text();
        if (this.checked) {
            $list.append('<li><a href="#">' + a + '</a><button class="closebutton" value="' + a + '">X</button></li>');
        } else {
            $("#itemList li:contains('" + a + "')").remove();
        }
    })
    $(document).on('click', '.closebutton', function () {
        var b = this.value;
        $("#itemList li:contains('" + b + "')").remove();
        $('label:contains("' + b + '")').find('.chkbox').prop('checked', false);
    });
});
<div id="items">
<ul id="itemList"></ul>
</div>
<div id="tab1" class="tab-pane">
<div id="ck-button">

     <!--html code should be like this no need to add span-->

    <input type="checkbox" id="one" class="chkbox"/> 
    <label for="one">First book</label>
</div>
<div id="ck-button">

    <input type="checkbox" class="chkbox" id="two" />
    <label for="two">  Second book</label>
</div>

var $list = $("#itemList");

$(".chkbox").change(function () {
var a = $('label[for="' + this.id + '"]').text();
var name=$(this).attr('id');
if (this.checked) {
    //add name attribute in the button with the id of checkbox
    $list.append('<li><a href="#">' + a + '</a> 
<button class="closebutton" value="' + a + '" name="'+name+'">X</button></li>');
} else {
    $("#itemList li:contains('" + a + "')").remove();
}
})

$(document).on('click', '.closebutton', function () {
var b = this.value;

$("#itemList li:contains('" + b + "')").remove();
//remove the checkbox whose id is same as the name attribute of button
$(':checkbox[id="' + this.name + '"]').removeAttr('checked');
});

DEMO DEMO

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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