javascript 删除 jQuery 中选中的复选框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26730986/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 06:24:31  来源:igfitidea点击:

Delete checked checkboxes in jQuery

javascriptjquerycheckbox

提问by Matt

I am trying to delete checked checkboxes in jQuery, but when i want to get state of checkbox i get error message.

我正在尝试删除 jQuery 中选中的复选框,但是当我想获取复选框的状态时,我收到错误消息。

jsfiddle

提琴手

    <div class="control-group">

<label class="checkbox"><input value="0" type="checkbox">Message 0</label>
<label class="checkbox"><input value="1" type="checkbox">Message 1</label>
<label class="checkbox"><input value="2" type="checkbox">Message 2</label>
<label class="checkbox"><input value="3" type="checkbox">Message 3</label>
<label class="checkbox"><input value="4" type="checkbox">Message 4</label>
<label class="checkbox"><input value="5" type="checkbox">Message 5</label>
<label class="checkbox"><input value="6" type="checkbox">Message 6</label>
<label class="checkbox"><input value="7" type="checkbox">Message 7</label>
<label class="checkbox"><input value="8" type="checkbox">Message 8</label>

</div> 


 <button class="btn" type="button" id="deleteAcc">Delete</button>

My jQuery code is:

我的 jQuery 代码是:

$("#deleteAcc").on("click",function(){      
    $(".control-group label.checkbox").each(function(){
        if (this.children(":first").is(':checked')) {
            this.remove();
        }
    }); 
});

回答by Ionic? Biz?u

You don't need eachmethod. Just select the checked inputs and remove the parents (the label elements containing the checkboxes):

你不需要each方法。只需选择选中的输入并删除父项(包含复选框的标签元素):

$("#deleteAcc").on("click", function() {
  $(".checkbox input:checked").parent().remove();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="control-group">

  <label class="checkbox">
    <input value="0" type="checkbox">Message 0</label>
  <label class="checkbox">
    <input value="1" type="checkbox">Message 1</label>
  <label class="checkbox">
    <input value="2" type="checkbox">Message 2</label>
  <label class="checkbox">
    <input value="3" type="checkbox">Message 3</label>
  <label class="checkbox">
    <input value="4" type="checkbox">Message 4</label>
  <label class="checkbox">
    <input value="5" type="checkbox">Message 5</label>
  <label class="checkbox">
    <input value="6" type="checkbox">Message 6</label>
  <label class="checkbox">
    <input value="7" type="checkbox">Message 7</label>
  <label class="checkbox">
    <input value="8" type="checkbox">Message 8</label>

</div>


<button class="btn" type="button" id="deleteAcc">Delete</button>

回答by Vinay

The actual issue is that the thiscontext in the $.eachclosure is a reference to the DOM node itself. One way you can fix this by wrapping thisin $:

实际问题是闭包中的this上下文$.each是对 DOM 节点本身的引用。您可以通过包装解决这个问题的一种方法this$

$("#deleteAcc").on("click",function(){
    $(".control-group label.checkbox").each(function(){
        if ($(this).children(":first").is(':checked')) {
            $(this).remove();
        }
    }); 
});

回答by Hafeez Hamza

Try this http://jsfiddle.net/zp3vwh1j/2/

试试这个 http://jsfiddle.net/zp3vwh1j/2/

$("#deleteAcc").on("click",function(){
    $("input:checkbox").each(function() {
        if ($(this).is(":checked")) {
            $(this).parent().remove();
        }
    });
});

回答by Hasnain Mehmood

$().ready(function () {
    $('body').on('click', '#deletebtn', function () {

        $("#example1 tr").each(function () {
            var rowSelector = $(this);
            if (rowSelector.find("input[type='checkbox']").prop('checked'))
            {
                //THE MARKUP SHOWING THE ID IS NOT AVAILABLE
                //POST A TABLE ENTRY TO CLEAR UP
                var id = rowSelector.find('td').first().next().html();
                var sendObj = {Id : id};
                //USE JSON OBJECT
                $.ajax({
                    url : "/page.aspx/deleteRecord",//CHANGE TO YOUR URL
                    dataType: "json",
                    data: sendObj,
                    type: "POST",
                    success: function () {
                        alert("entry deleted");
                    }
                });
                rowSelector.remove();
            }
        });

    });
});