user39980
user39980

Reputation:

Serialize all fields

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?

Upvotes: 0

Views: 975

Answers (2)

KyleFarris
KyleFarris

Reputation: 17548

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);
}

Upvotes: 0

karim79
karim79

Reputation: 342635

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.

Upvotes: 2

Related Questions