Check if checkbox is checked with jQuery

后端 未结 23 3051
忘了有多久
忘了有多久 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-21 23:54

    Toggle checkbox checked

    $("#checkall").click(function(){
        $("input:checkbox").prop( 'checked',$(this).is(":checked") );
    })
    
    0 讨论(0)
  • 2020-11-21 23:55

    Using this code you can check at least one checkbox is selected or not in different checkbox groups or from multiple checkboxes. Using this you can not require to remove IDs or dynamic IDs. This code work with the same IDs.

    Reference Link

    <label class="control-label col-sm-4">Check Box 2</label>
        <input type="checkbox" name="checkbox2" id="checkbox21" value=ck1 /> ck1<br />
        <input type="checkbox" name="checkbox2" id="checkbox22" value=ck2 /> ck2<br />
    
    <label class="control-label col-sm-4">Check Box 3</label>
        <input type="checkbox" name="checkbox3" id="checkbox31" value=ck3 /> ck3<br />
        <input type="checkbox" name="checkbox3" id="checkbox32" value=ck4 /> ck4<br />
    
    <script>
    function checkFormData() {
        if (!$('input[name=checkbox2]:checked').length > 0) {
            document.getElementById("errMessage").innerHTML = "Check Box 2 can not be null";
            return false;
        }
        if (!$('input[name=checkbox3]:checked').length > 0) {
            document.getElementById("errMessage").innerHTML = "Check Box 3 can not be null";
            return false;
        }
        alert("Success");
        return true;
    }
    </script>
    
    0 讨论(0)
  • 2020-11-21 23:56

    All following methods are useful:

    $('#checkbox').is(":checked")
    
    $('#checkbox').prop('checked')
    
    $('#checkbox')[0].checked
    
    $('#checkbox').get(0).checked
    

    It is recommended that DOMelement or inline "this.checked" should be avoided instead jQuery on method should be used event listener.

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

    jQuery code to check whether the checkbox is checked or not:

    if($('input[name="checkBoxName"]').is(':checked'))
    {
      // checked
    }else
    {
     // unchecked
    }
    

    Alternatively:

    if($('input[name="checkBoxName"]:checked'))
    {
        // checked
    }else{
      // unchecked
    }
    
    0 讨论(0)
  • 2020-11-22 00:00
    $('#checkbox').is(':checked'); 
    

    The above code returns true if the checkbox is checked or false if not.

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

    Since it's mid 2019 and jQuery sometimes takes a backseat to things like VueJS, React etc. Here's a pure vanilla Javascript onload listener option:

    <script>
      // Replace 'admincheckbox' both variable and ID with whatever suits.
    
      window.onload = function() {
        const admincheckbox = document.getElementById("admincheckbox");
        admincheckbox.addEventListener('click', function() {
          if(admincheckbox.checked){
            alert('Checked');
          } else {
            alert('Unchecked');
          }
        });
      }
    </script>
    
    0 讨论(0)
提交回复
热议问题