$(".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
大家讲道理2017-05-18 11:03:31
$(".checkboxs_yy").click(function(){
if($(this).is(":checked")){
console.log("选中");
}else{
console.log("未选中");
}
});
为情所困2017-05-18 11:03:31
Use $(this).prop('checked') instead of $(this).attr('checked') to get dynamically variable attributes.
我想大声告诉你2017-05-18 11:03:31
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