I need to map an array of inputs to pass via an ajax call. Inputs look like this:
<input type="hidden" name="sizes[0][id]" value="0" class="sizes">
<input type="hidden" name="sizes[0][name]" value="Small" class="sizes">
<input type="hidden" name="sizes[1][id]" value="1" class="sizes">
<input type="hidden" name="sizes[1][name]" value="Medium" class="sizes">
<input type="hidden" name="sizes[2][id]" value="2" class="sizes">
<input type="hidden" name="sizes[2][name]" value="Large" class="sizes">
I'm trying to map it like the following, but it is not working as it doesn't grab the id or name field.
var sizes = $('input.sizes').map(function(){ return $(this).val(); }).get();
Maybe this needs to be recursive somehow?
Edit: The output needs to work with the following code:
$.ajax({
type : "POST",
cache : false,
data : {
sizes: sizes,
...
},
url : 'http://what.does.the.fox/say',
dataType : 'json',
...
});
Another update: This should create an array of objects that resemble the input name tag. ie:
var sizes = [
{ id: 0, name: 'Small' },
{ id: 1, name: 'Medium' },
{ id: 2, name: 'Large' }
];
sizes<- look like?