I have a number of cases where I would like to use fieldSerialize() to select a subset of form fields, but it would be more useful to get an array of name value pairs (just like formToArray()) instead of a baked querystring. To make the API consistent, I propose adding fieldToArray() to complement formToArray().
This can be done by moving the guts of fieldSerialize() to fieldToArray(), and calling fieldToArray() from fieldSerialize().
Here's the code:
/**
* Serializes all field elements in the jQuery object into a query string.
* This method will return a string in the format: name1=value1&name2=value2
*/
$.fn.fieldSerialize = function(successful) {
var a = this.fieldCollect(successful);
//hand off to jQuery.param for proper encoding
return $.param(a);
};
/**
* Serializes all field elements in the jQuery object into an array of key/value pairs.
*/
$.fn.fieldToArray = function(successful)
{
var a = [];
this.each(function() {
var n = this.name;
if (!n) {
return;
}
var v = $.fieldValue(this, successful);
if (v && v.constructor == Array) {
for (var i=0,max=v.length; i < max; i++) {
a.push({name: n, value: v[i]});
}
}
else if (v !== null && typeof v != 'undefined') {
a.push({name: this.name, value: v});
}
});
return a;
};
Is there an appetite for this kind of thing?