Check if checkbox is checked with jQuery

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

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

    
    
    

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

    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;
    

提交回复
热议问题