Best way to get all selected checkboxes VALUES in

2019-01-01 11:26发布

问题:

This question already has an answer here:

  • How to retrieve checkboxes values in jQuery 16 answers

I am looking to get all checkboxes\' VALUE which have been selected through jQuery.

回答1:

You want the :checkbox:checked selector and map to create an array of the values:

var checkedValues = $(\'input:checkbox:checked\').map(function() {
    return this.value;
}).get();

If your checkboxes have a shared class it would be faster to use that instead, eg. $(\'.mycheckboxes:checked\'), or for a common name $(\'input[name=\"Foo\"]:checked\')