tags:

views:

55

answers:

2

If form is submitted to a function using onsubmit(), how can I get an array of the values of all checkboxes with name='values[]' ?

EDIT: originally I mentioned radio buttons, this is wrong - checkboxes is correct.

EDIT: line below only gets the value of the first one, obviously

$("input[name='values[]']:checked").val()

Also, how can I get the number of checkboxes that were checked when the form was submitted?

+2  A: 

val returns a single value. To get all values, use map:

var selectedValues = $("input[name^='values']:checked").map(
       function(){ return $(this).val(); }).get();

This returns an array with all selected values. To get a dash-delimited string:

var dashed = selectedValues.join('-');
Kobi
This returns nothing, just blank value
stef
Sorry, forgot the `:checked`, and too many single quotes. Here's a working example: http://jsbin.com/abovi
Kobi
Perfect! Is there any way to replace the comma with a dash?
stef
A: 

try

$('#formId').serializeArray();
I.devries
A nice idea, but lacks filtering to radio buttons and checkboxes.
Joel Potter