If the posting parameters are declared in the name element attribute of each field on your form, you can use the $.fn.serialize
(commented on by @Zuul). The method will return usable URL text in the "data"
property that is inside the $.ajax
( $.post
, etc) method option object.
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 serialize it with $.fn.serialize
:
$("form").serialize()
and we get:
"Tipo=Leite"
If the case is otherwise, you can construct an object containing each post parameter as property by assigning the element identifier of its field itself. With this you can go through each property and build a new object with the values of the elements.
* The identifier is equivalent to the value that is declared in the id attribute of an element.
var dataBuffer = {
"email": "news-email"
, "nome": "news-nome"
};
var dataToPost = {};
for (var param in dataBuffer)
dataToPost[param] = $('#'+dataBuffer[param]).val();
sendData(dataToPost);