Error in serialize jquery

Asked

Viewed 67 times

0

var Dados = {};

Dados.email = jQuery("#email").val();
Dados.nome= jQuery("#nome").val();

var urlparams = Dados.serialize();

My console returns Dados.serialize is not a Function there is some error in the code?

  • That’s because Dados is not a jQuery object. And if you do jQuery(Dados).serialize()?

  • @Andersoncarloswoss sorry I didn’t understand your question

  • The parameters come blank

  • var Data = {}; Data.email = "email"; Data.name = "name"; var urlparams = jQuery(Data). serialize(); Alert(urlparams);

1 answer

5


You are using the wrong method. The method .serialize(), according to the documentation, it is used to create a string in the pattern URL-encoded through a jQuery object that has selected form fields, such as inputs, selects and textareas, or the form itself.

What you seek is the function $.param(), which creates a serialized representation of the data of an object:

const string = $.param({
  name: 'Luiz Felipe',
  nickname: 'lffg'
});

console.log(string);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Reference:

  • 1

    +1 Good answer! I would just add that the serialize should be used, or in a form, or in a selection of input, textarea and select , as shown in the documentation you mentioned: $( "input, textarea, select" ).serialize();

  • Thank you! I will edit the reply soon. However, turn me more on to the function $.param(), since she is the focus of the question, and not .serialize(). :)

  • I understand, I only found valid in this case because it is cited the documentation of serialize and would be more complete for those who read. Even if the function $.param be the focus. D

  • 1

    I edited it. Thanks! ;)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.