How do I check whether a checkbox is checked in jQuery?

前端 未结 30 3439
花落未央
花落未央 2020-11-21 04:44

I need to check the checked property of a checkbox and perform an action based on the checked property using jQuery.

For example, if the age checkbox is

30条回答
  •  再見小時候
    2020-11-21 05:01

    Since jQuery 1.6, the behavior of jQuery.attr() has changed and users are encouraged not to use it to retrieve an element's checked state. Instead, you should use jQuery.prop():

    $("#txtAge").toggle(
        $("#isAgeSelected").prop("checked") // For checked attribute it returns true/false;
                                            // Return value changes with checkbox state
    );
    

    Two other possibilities are:

    $("#txtAge").get(0).checked
    $("#txtAge").is(":checked")
    

提交回复
热议问题