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>
相關文章
相關標籤/搜索