16

I was dabbling with the form serialize but surprisingly its not serializing the form. Here is the code:

<div id="content">
</div>
<form  id= "myform">
    <input type="text" id="inp"value="mytext">
    <input type="button" id="btn" value="serialize"/>
</form>

Here is the jQuery code I am working with:

$("form").submit(function(e){
    e.preventDefault();
    var v= $(this).serialize();
    console.log(v);
});

Here is the fiddle

Tassadaque
  • 8,129
  • 13
  • 57
  • 89

2 Answers2

64

You need a name attribute on your input fields. Otherwise they're ignored by jQuery's .serialize().

Here's a quote from the docs:

Note: Only "successful controls" are serialized to the string. No submit button value is serialized since the form was not submitted using a button. For a form element's value to be included in the serialized string, the element must have a name attribute. Values from checkboxes and radio buttons (inputs of type "radio" or "checkbox") are included only if they are checked. Data from file select elements is not serialized.

Here's your fiddle with a name attribute: http://jsfiddle.net/6fgUg/28/

Joseph Silber
  • 214,931
  • 59
  • 362
  • 292
4

You have to give a name to the input HTML element, for example:

<form id="myform"> 
    <input type="text" id="inp" name="inp" value="mytext">
    <input type="button" id="btn" name="btn" value="serialize"/>
</form>
Yanny
  • 41
  • 1