0

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!

Bruce
  • 2,406
  • 5
  • 29
  • 35
  • 1
    Can you share your form code? input field must have a name for serialize. – Anoop Sep 19 '12 at 13:53
  • You, sir, are a genius! Thank you very much. That was exactly the problem.. If you want to put your comment in a separate answer, I will mark it correct.. – Bruce Sep 19 '12 at 13:59

3 Answers3

1

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
});
spekdrum
  • 1,559
  • 2
  • 11
  • 15
1

jQuery .serialize() should do the trick. You can simply use $('form').serialize() to get form datas.

Here is a demo, http://jsfiddle.net/muthkum/x6n2e/1/. One is called before submitting form. Another is called when you click submit button. Both works.

Muthu Kumaran
  • 17,682
  • 5
  • 47
  • 70
0

As simple as

$( YOUR_INPUTS_TO_BE_SERIALIZED ).serialize() ...

documentation HERE for further infos

Note that you might need to transform the ouput string to make it fit hash granted chars :

THIS THREAD may help you

Community
  • 1
  • 1
Stphane
  • 3,368
  • 5
  • 32
  • 47