Check if at least one checkbox is checked using jQuery

前端 未结 9 1162
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-31 11:31

I have five checkboxes. Using jQuery, how do I check if at least one of them is checked?



<
9条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-31 12:33

    is() can do this, and is arguably the only acceptable use of is(":checked"):

    From the jQuery docs, http://api.jquery.com/is/:

    Check the current matched set of elements against a selector, element, or jQuery object and return true if at least one of these elements matches the given arguments.

    alert($("input[name='service[]']").is(":checked"));
    

    Example: http://jsfiddle.net/AndyE/bytVX/1/ (based on the fiddle by Brandon Gano)

    Alternatively, and potentially faster, you can pass a function to is():

    $("input[name='service[]']").is(function () {
        return this.checked;
    });
    

提交回复
热议问题