I have an object that is inside of a form.
His value is assigned to an ajax call like this
var $$ = $(this);
if (typeof settings.data == 'string') {
data = settings.data + '&' + this.name + '=' + $$.val();
} else if (typeof settings.data == 'object') {
data = settings.data;
data[this.name] = $$.val();
}
I want to get the parent form of this element, and to loop through all inputs and add that too to the data to be used on an Ajax call.
I want the input name to be the key for the data array.
Probably something like:
var form = $$.parents('form:first');
and next thing to loop through the inputs and attach to data.
How to do that using jQuery?
I think you’re reinventing the wheel here, there’s already a
.serialize()method specifically for this, like this:Or if you want an object representation you can loop through/add to, you can use
.serializeArray()then manipulate it before passing it as the data option. It’s unclear exactly what you’re after, but it sounds like you just want to pass the entire form as the data argument to$.ajax()or a short-hand version, in which case.serialize()would be perfect.