I want to create a string representation of certain html forms for storing in the hash portion of the url.
I was lazily hoping that there would be such a method and jquery. serialize() seems to do the trick, except that in all the examples I've seen, it uses the 'this' object of a form submission. On my site the forms aren't getting submitted, I just want to be able to serialize them. I've tried:
params['formParams'] = $("#"+currentSearchForm).serialize();
but I just get an empty string instead of the serialized form.
Should I spoof a form submission and generate my serialized string in it, and then cancel the default event or something? Seems a little contrived..?
Alternatively could be I'm just doing serialize wrong..
Thanks for your help!
First make sure your form elements have the 'name' atribute. This may be the cause of non working serialize.
If you want to do this on submit and then cancel the submit you can do this:
$("#formid").submit(function() {
//whatever
return false; //<- this cancels the submit
});