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

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

    1) If your HTML markup is:

    
    

    attr used:

    $(element).attr("checked"); // Will give you undefined as initial value of checkbox is not set
    

    If prop is used:

    $(element).prop("checked"); // Will give you false whether or not initial value is set
    

    2) If your HTML markup is:

     // May be like this also  checked="true"
    

    attr used:

    $(element).attr("checked") // Will return checked whether it is checked="true"
    

    Prop used:

    $(element).prop("checked") // Will return true whether checked="checked"
    

提交回复
热议问题