search

Home  >  Q&A  >  body text

javascript - Time triggered when checkbox is checked and unchecked

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

PHPzPHPz2753 days ago760

reply all(4)I'll reply

  • 大家讲道理

    大家讲道理2017-05-18 11:03:31

    $(".checkboxs_yy").click(function(){
        if($(this).is(":checked")){
            console.log("选中");
        }else{
            console.log("未选中");
        }
    });

    reply
    0
  • 巴扎黑

    巴扎黑2017-05-18 11:03:31

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

    reply
    0
  • 为情所困

    为情所困2017-05-18 11:03:31

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

    reply
    0
  • 我想大声告诉你

    我想大声告诉你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

    reply
    0
  • Cancelreply