Check if checkbox is checked with jQuery

后端 未结 23 3049
忘了有多久
忘了有多久 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:38

    You can use any of the following recommended codes by jquery.

    if ( elem.checked ) {};
    if ( $( elem ).prop( "checked" ) ) {};
    if ( $( elem ).is( ":checked" ) ) {};
    
    0 讨论(0)
  • 2020-11-21 23:39

    You can use this code,

    if($("#checkboxId").is(':checked')){
         // Code in the case checkbox is checked.
    } else {
         // Code in the case checkbox is NOT checked.
    }
    
    0 讨论(0)
  • 2020-11-21 23:42

    IDs must be unique in your document, meaning that you shouldn't do this:

    <input type="checkbox" name="chk[]" id="chk[]" value="Apples" />
    <input type="checkbox" name="chk[]" id="chk[]" value="Bananas" />
    

    Instead, drop the ID, and then select them by name, or by a containing element:

    <fieldset id="checkArray">
        <input type="checkbox" name="chk[]" value="Apples" />
    
        <input type="checkbox" name="chk[]" value="Bananas" />
    </fieldset>
    

    And now the jQuery:

    var atLeastOneIsChecked = $('#checkArray:checkbox:checked').length > 0;
    //there should be no space between identifier and selector
    
    // or, without the container:
    
    var atLeastOneIsChecked = $('input[name="chk[]"]:checked').length > 0;
    
    0 讨论(0)
  • 2020-11-21 23:42

    As per the jQuery documentation there are following ways to check if a checkbox is checked or not. Lets consider a checkbox for example (Check Working jsfiddle with all examples)

    <input type="checkbox" name="mycheckbox" id="mycheckbox" />
    <br><br>
    <input type="button" id="test-with-checked" value="Test with checked" />
    <input type="button" id="test-with-is" value="Test with is" />
    <input type="button" id="test-with-prop" value="Test with prop" />
    

    Example 1 - With checked

    $("#test-with-checked").on("click", function(){
        if(mycheckbox.checked) {
            alert("Checkbox is checked.");
        } else {
            alert("Checkbox is unchecked.");
        }
    }); 
    

    Example 2 - With jQuery is, NOTE - :checked

    var check;
    $("#test-with-is").on("click", function(){
        check = $("#mycheckbox").is(":checked");
        if(check) {
            alert("Checkbox is checked.");
        } else {
            alert("Checkbox is unchecked.");
        }
    }); 
    

    Example 3 - With jQuery prop

    var check;
    $("#test-with-prop").on("click", function(){
        check = $("#mycheckbox").prop("checked");
        if(check) {
             alert("Checkbox is checked.");
        } else {
            alert("Checkbox is unchecked.");
        }
    }); 
    

    Check Working jsfiddle

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

    Something like this can help

    togglecheckBoxs =  function( objCheckBox ) {
    
        var boolAllChecked = true;
    
        if( false == objCheckBox.checked ) {
            $('#checkAll').prop( 'checked',false );
        } else {
            $( 'input[id^="someIds_"]' ).each( function( chkboxIndex, chkbox ) {
                if( false == chkbox.checked ) {
                    $('#checkAll').prop( 'checked',false );
                    boolAllChecked = false;
                }
            });
    
            if( true == boolAllChecked ) {
                $('#checkAll').prop( 'checked',true );
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-21 23:43
    $('#' + id).is(":checked")
    

    That gets if the checkbox is checked.

    For an array of checkboxes with the same name you can get the list of checked ones by:

    var $boxes = $('input[name=thename]:checked');
    

    Then to loop through them and see what's checked you can do:

    $boxes.each(function(){
        // Do stuff here with this
    });
    

    To find how many are checked you can do:

    $boxes.length;
    
    0 讨论(0)
提交回复
热议问题