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

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

    <input type="checkbox"  />
    

    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:

     <input type="checkbox"  checked="checked" />// 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"
    
    0 讨论(0)
  • 2020-11-21 05:17

    This worked for me:

    $get("isAgeSelected ").checked == true
    

    Where isAgeSelected is the id of the control.

    Also, @karim79's answer works fine. I am not sure what I missed at the time I tested it.

    Note, this is answer uses Microsoft Ajax, not jQuery

    0 讨论(0)
  • 2020-11-21 05:19

    If you are using an updated version of jquery, you must go for .prop method to resolve your issue:

    $('#isAgeSelected').prop('checked') will return true if checked and false if unchecked. I confirmed it and I came across this issue earlier. $('#isAgeSelected').attr('checked') and $('#isAgeSelected').is('checked') is returning undefined which is not a worthy answer for the situation. So do as given below.

    if($('#isAgeSelected').prop('checked')) {
        $("#txtAge").show();
    } else {
        $("#txtAge").hide();
    }
    

    Hope it helps :)- Thanks.

    0 讨论(0)
  • 2020-11-21 05:20

    Using jQuery > 1.6

    <input type="checkbox" value="1" name="checkMeOut" id="checkMeOut" checked="checked" />
    
    // traditional attr
    $('#checkMeOut').attr('checked'); // "checked"
    // new property method
    $('#checkMeOut').prop('checked'); // true
    

    Using the new property method:

    if($('#checkMeOut').prop('checked')) {
        // something when checked
    } else {
        // something else when not
    }
    
    0 讨论(0)
  • 2020-11-21 05:20

    I ran in to the exact same issue. I have an ASP.NET checkbox

    <asp:CheckBox ID="chkBox1" CssClass='cssChkBox1' runat="server" />
    

    In the jQuery code I used the following selector to check if the checkbox was checked or not, and it seems to work like a charm.

    if ($("'.cssChkBox1 input[type=checkbox]'").is(':checked'))
    { ... } else { ... }
    

    I'm sure you can also use the ID instead of the CssClass,

    if ($("'#cssChkBox1 input[type=checkbox]'").is(':checked'))
    { ... } else { ... }
    

    I hope this helps you.

    0 讨论(0)
  • 2020-11-21 05:23

    You can try the change event of checkbox to track the :checked state change.

    $("#isAgeSelected").on('change', function() {
      if ($("#isAgeSelected").is(':checked'))
        alert("checked");
      else {
        alert("unchecked");
      }
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <input type="checkbox" id="isAgeSelected" />
    <div id="txtAge" style="display:none">
      Age is selected
    </div>

    0 讨论(0)
提交回复
热议问题