获取checkbox选中状态的三种方式

凨影 1034

使用jquery方式获取状态 第一种方式:
<input type="checkbox">
<script>
$("[type=checkbox]").click(function () {
    alert($(this).is(":checked"));
})
</script>
使用jquery方式获取状态 第二种方式:
<input type="checkbox" id="checkbox">
<script>
$("#checkbox").click(function () {
    alert($(this).is(":checked"));
    /*
    //其他使用JQuery获取状态的几种方法
    $(this).attr("checked"); //看版本1.6+返回:”checked”或”undefined”
    $(this).prop("checked"); //16+:true/false
    $(this).is(":checked"); //所有版本:true/false
    $(this).prop("checked","checked");
    
    //query赋值checked的几种写法:
    $(this).attr("checked","checked");
    $(this).attr("checked",true);
    $(this).prop("checked",true);
    $(this).prop({checked:true});
    
    $(this).prop("checked",function(){
        return true;//函数返回true或false
    });
    */
})
</script>
使用原生js click函数获取状态:
<input type="checkbox" onclick="checkbox(this)">
<script>
function checkbox(obj){
    alert(obj.checked);
}
</script>
联系QQ:77798085 赞助请点这里
全部回复 (0)