javascript - Time triggered when checkbox is checked and unchecked
PHPz
PHPz 2017-05-18 11:01:31
0
4
775

$(".checkboxs_yy").click(function(){

       if($(this).attr("checked")==true){
           console.log("选中");
       }else{
           console.log("未选中");
       } 
    });

.checkboxs_yy is a checkbox. I want it to trigger a time when it is checked, and trigger another event when it is not checked. Why is this thing I wrote always unchecked? How should I change it

PHPz
PHPz

学习是最好的投资!

reply all(4)
大家讲道理
$(".checkboxs_yy").click(function(){
    if($(this).is(":checked")){
        console.log("选中");
    }else{
        console.log("未选中");
    }
});
巴扎黑

$(this).is(":checked")

为情所困

Use $(this).prop('checked') instead of $(this).attr('checked') to get dynamically variable attributes.

我想大声告诉你

attr() is not suitable to be used to determine whether it is selected under checkbox. You can use .is(":checked") or .prop("checked", true) to determine whether it is selected.
There are relevant instructions in the jQuery API. jQuery 1.6+ needs to use prop, especially the judgment of the checked attribute of checkBox.
At the same time, .is(":checked") is suitable for all versions

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template