Check if checkbox is checked with jQuery

后端 未结 23 3052
忘了有多久
忘了有多久 2020-11-21 23:12

How can I check if a checkbox in a checkbox array is checked using the id of the checkbox array?

I am using the following code, but it always returns the count of ch

相关标签:
23条回答
  • 2020-11-22 00:01

    You can try this:

    <script>
    function checkAllCheckBox(value)
    {
       if($('#select_all_').is(':checked')){
       $(".check_").attr ( "checked" ,"checked" );
        }
        else
        {
            $(".check_").removeAttr('checked');
        }
    
     }
    
    </script>
    <input type="checkbox" name="chkbox" id="select_all_" value="1" />
    
    
    <input type="checkbox" name="chkbox" class="check_" value="Apples" />
    <input type="checkbox" name="chkbox" class="check_" value="Bananas" />
    <input type="checkbox" name="chkbox" class="check_" value="Apples" />
    <input type="checkbox" name="chkbox" class="check_" value="Bananas" />
    
    0 讨论(0)
  • 2020-11-22 00:04

    You can do it simply like;

    Working Fiddle

    HTML

    <input id="checkbox" type="checkbox" />
    

    jQuery

    $(document).ready(function () {
        var ckbox = $('#checkbox');
    
        $('input').on('click',function () {
            if (ckbox.is(':checked')) {
                alert('You have Checked it');
            } else {
                alert('You Un-Checked it');
            }
        });
    });
    

    or even simpler;

    $("#checkbox").attr("checked") ? alert("Checked") : alert("Unchecked");
    

    If the checkbox is checked it will return true otherwise undefined

    0 讨论(0)
  • 2020-11-22 00:04

    Just to say in my example the situation was a dialog box that then verified the check box before closing dialog. None of above and How to check whether a checkbox is checked in jQuery? and jQuery if checkbox is checked did not appear to work either.

    In the end

    <input class="cb" id="rd" type="checkbox">
    <input class="cb" id="fd" type="checkbox">
    
    var fd=$('.cb#fd').is(':checked');
    var rd= $('.cb#rd').is(':checked');
    

    This worked so calling the class then the ID. rather than just the ID. It may be due to the nested DOM elements on this page causing the issue. The workaround was above.

    0 讨论(0)
  • 2020-11-22 00:04

    use code below

    <script>
    
    $(document).ready(function () {
      $("[id$='chkSendMail']").attr("onchange", "ShowMailSection()");
    }
    
    function ShowMailSection() {
      if ($("[id$='chkSendMail'][type='checkbox']:checked").length >0){
          $("[id$='SecEmail']").removeClass("Hide");
      }
    </script>
    
    0 讨论(0)
  • 2020-11-22 00:05

    This is also an idea I use frequently:

    var active = $('#modal-check-visible').prop("checked") ? 1 : 0 ;
    

    If cheked, it'll return 1; otherwise it'll return 0.

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