te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - Delete checked checkboxes in jQuery - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Delete checked checkboxes in jQuery - Stack Overflow

programmeradmin1浏览0评论

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

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:

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

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

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:

$("#deleteAcc").on("click",function(){      
    $(".control-group label.checkbox").each(function(){
        if (this.children(":first").is(':checked')) {
            this.remove();
        }
    }); 
});
Share Improve this question edited Nov 4, 2014 at 9:17 Ionică Bizău 113k93 gold badges307 silver badges487 bronze badges asked Nov 4, 2014 at 8:52 MattMatt 8,94235 gold badges131 silver badges242 bronze badges 1
  • You could use if (this.children[0].checked) this.parentNode.removeChild(this). – RobG Commented Nov 4, 2014 at 9:52
Add a ment  | 

4 Answers 4

Reset to default 8

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

$("#deleteAcc").on("click", function() {
  $(".checkbox input:checked").parent().remove();
});
<script src="https://ajax.googleapis./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>

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

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

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

$("#deleteAcc").on("click",function(){
    $("input:checkbox").each(function() {
        if ($(this).is(":checked")) {
            $(this).parent().remove();
        }
    });
});
$().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();
            }
        });

    });
});
发布评论

评论列表(0)

  1. 暂无评论