jQuery: how do I return true if any checkbox is selected in an array of checkboxes?
my checkboxs each 开发者_如何学Clook like this:
<input id="bulk_selected_" name="bulk_selected[]" type="checkbox" value="159">
Use an attribute-equals selector and .is()
, like this:
return $("input[name='bulk_selected[]']").is(":checked");
//or...
return $("input[name='bulk_selected[]']:checked").length;
.is()
will return true
is any of the elements match the selector. Or a bit faster version:
return $("input[name='bulk_selected[]']").filter(function() {
return this.checked;
}).length > 0;
return jQuery('[name="bulk_selected[]"]:checked').size()
If none are checked, then it will return 0
(a false value) otherwise it will return a positive number (which will be true).
精彩评论