1

I got a problem when using the serialize() jQuery function:

<form role="form" id="enter-image-detail-form" method="post">
  <div class="form-group">
    <input type="text" class="form-control" id="image-name" placeholder="Enter image name" value="test">
  </div>
  <button type="submit" class="btn btn-default btn-success btn-block">Save</button>
</form>
$('body').on('submit', '#enter-image-detail-form', function(e){
  console.log( $(this).serialize() );
  e.preventDefault();
});

It returns an empty string. Thanks in advance.

1
  • @T.J.Crowder you're right, my bad. Commented Feb 23, 2017 at 15:53

1 Answer 1

6

You need to add name attribute for form elements since serialize() method generates form elements which has name attribute.

<input name="image-name" type="text" class="form-control" id="image-name" placeholder="Enter image name" value="test">
<!--   ^^^^^^^^^^^^^^^^^                                                  --->

From docs :

For a form element's value to be included in the serialized string, the element must have a name attribute.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.