views:

210

answers:

2

Hey,

I checked out the jquery serialize docs and I am trying to find the best way too serialize all fields in my form and then print the output, the demo has something like:

  function showValues() {
       var str = $("form").serialize();
       $("#results").text(str);
     }

     $(":checkbox, :radio").click(showValues);
     $("select").change(showValues);
     showValues();
 });

Even then call the serialize on form submit, use return false and have it show them.

Thoughts?

+1  A: 

If you use the forms plugin, you can do this:

var formData = jQuery('form').formSerialize();
alert(formData);

That will serialize all the elements in the form.

karim79
+1  A: 

Are you putting inside the document.ready method? It looks like you are ending that way... It should be like:

$(function() {
    $(":checkbox, :radio").click(showValues);
    $("select").change(showValues);
    $('form').bind('submit',function() { showValues(); return false; });
    showValues();
});

function showValues() {
    var str = $("form").serialize();
    $("#results").text(str);
}
KyleFarris