$(".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
$(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