views:

37

answers:

3

I'm using:

var data = $("form :input[value]");

To get all the values in a form and passing the data to an ajax script.

My problem is it doesn't work with RADIO buttons - it always sends the last value for the radio group.

Any ideas on how to get the checked radio button along with all the other form values?

+1  A: 

There's a function built-in for this, just call .serialize(), like this:

var data = $("form").serialize();

You can see how it's appropriately filtering out these elements here.

Nick Craver
That worked, thanks. I made a change to ignore blank fields:var data = $("form :input[value!='']").serialize();
Ricky
+1  A: 

You are maybe looking for .serialize() method.

var data = $('form').serialize();

Note: .serialize() only serializes input controls which have a name attribute.

Reference: .serialize()

jAndy
A: 

for radio buttons you need to inspect the checked attribute.

Am