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

前端 未结 30 3533
花落未央
花落未央 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:09

    Using the Click event handler for the checkbox property is unreliable, as the checked property can change during the execution of the event handler itself!

    Ideally, you'd want to put your code into a change event handler such as it is fired every time the value of the check box is changed (independent of how it's done so).

    $('#isAgeSelected').bind('change', function () {
    
       if ($(this).is(':checked'))
         $("#txtAge").show();
       else
         $("#txtAge").hide();
    });
    

提交回复
热议问题