If the posting parameters are declared in the attribute name element of each field of your form, you can use the method $.fn.serialize
(commented by @Zuul). The method will return a usable URL text in the property "data"
which lies within the options object of the method $.ajax
($.post
, etc.).
To better understand this method, let’s assume that we have a form like this:
<form>
<select name="Tipo">
<option>Caixa</option>
<option selected>Leite</option>
</select>
</form>
Then we serialized him with $.fn.serialize
:
$("form").serialize()
and we get:
"Tipo=Leite"
If the case is different, you can build an object containing each posting parameter as property by assigning the element identifier of your field itself. With this you can go through each property and build a new object with the values of the elements.
*The identifier is the value that is declared in the attribute id of a single element.
var dataBuffer = {
"email": "news-email"
, "nome": "news-nome"
};
var dataToPost = {};
for (var param in dataBuffer)
dataToPost[param] = $('#'+dataBuffer[param]).val();
sendData(dataToPost);
Take a look at: http://api.jquery.com/serialize/.
– Zuul
They’re all the same kind ? you can use
$( "#minhaDiv" ).find( "input :text" )
– Lucas Queiroz Ribeiro
Yes, I get them all with the class . input however my doubt is to take the value of each and put in order in my json that I will send.
– Steve Angello