Check if at least one checkbox is checked using jQuery

前端 未结 9 1205
爱一瞬间的悲伤
爱一瞬间的悲伤 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:18

    var checkboxes = document.getElementsByName("service[]");
    if ([].some.call(checkboxes, function () { return this.checked; })) {
      // code
    }
    

    What you want is simple, get all the elements with the name, then run some code if some of those elements are checked.

    No need for jQuery.

    You may need an ES5 shim for legacy browsers though

提交回复
热议问题