jquery判断checkbox(复选框)是否选中

判断checkbox是否被选中大概有下面四种方法javascript

$(".check_class").prop("checked") 返回booleanhtml

$(".check_class").is(":checked")返回booleanjava

$(".check_class").attr("checked"); 返回boolean this

循环的话还能够:code

$(".check_class").each(function(){htm

    this.checked  返回 booleanip

}input

<html>
<head>
<input type="checkbox" class="">
</head>
<body>
<div>
    <input type="button" class="check_all" value="全选">
    <input type="button" class="check_opposite" value="反选">

</div>
<div>
    <input type="checkbox" class="check_class" value="small">小
    <input type="checkbox" class="check_class" value="middel">中
    <input type="checkbox" class="check_class" value="big">大
</div>

<script type="text/javascript">
    $(function(){
        $(".check_all").click(function(){
            $(".check_class").prop("checked", this.checked);
            //$(".check_class").attr("checked","checked");--有的时候无论用,缘由详见上一篇(.prop()和 .attr()区别)
        });
        $(".check_opposite").click(function(){
            $(".check_class").each(function(){
                $(this).prop("checked", !this.checked);
                //$(this).attr("checked",!$(this).attr("checked"));--有的时候无论用,缘由详见上一篇(.prop()和 .attr()区别)

            });
            $(".check_class").prop("checked", this.checked);
        });
    })
</script> 
</body>

</html>
相关文章
相关标签/搜索