jquery判断checkbox是否选中及改变checkbox状态

在用jquery判断checkbox 的选中上.

在用的$().attr('checked')做全选和反选的时候出现一个问题,点第一二次 可以选中和全不选, 但第三次的时候就没有反应了

 

之后用了$().prop()  就可以了

//全选和全不选
    $("#allorone_check").click(function(){
        if(!$(this).hasClass("selectall")){
            console.log($("#lxyzlist input[type=checkbox]"));
            $(".tbodylist input:checkbox").prop("checked",true);
            $(this).addClass("selectall");
        }else{
            $("#lxyzlist input:checkbox").prop("checked",false);
            $(this).removeClass("selectall");
        }
    });
    //反选
    $("#invert_check").click(function(){
         $(".tbodylist input[type=checkbox]:checkbox").each(function () {   
                //$(this).attr("checked", !$(this).prop("checked"));   
                 $(this).prop("checked", function(index, attr){
                    return !attr;
                });
         }); 
    });

 

posted @ 2016-10-21 10:25  DemonGao  阅读(502)  评论(0编辑  收藏  举报