| 1 |
// $Id: hierarchical_select_formtoarray.js,v 1.2 2008/05/01 12:11:35 wimleers Exp $
|
| 2 |
|
| 3 |
/**
|
| 4 |
* @file
|
| 5 |
* Contains the formToArray method and the method it depends on. Taken from
|
| 6 |
* jQuery Form Plugin 2.12. (http://www.malsup.com/jquery/form/)
|
| 7 |
*/
|
| 8 |
|
| 9 |
(function ($) {
|
| 10 |
|
| 11 |
/**
|
| 12 |
* formToArray() gathers form element data into an array of objects that can
|
| 13 |
* be passed to any of the following ajax functions: $.get, $.post, or load.
|
| 14 |
* Each object in the array has both a 'name' and 'value' property. An example of
|
| 15 |
* an array for a simple login form might be:
|
| 16 |
*
|
| 17 |
* [ { name: 'username', value: 'jresig' }, { name: 'password', value: 'secret' } ]
|
| 18 |
*
|
| 19 |
* It is this array that is passed to pre-submit callback functions provided to the
|
| 20 |
* ajaxSubmit() and ajaxForm() methods.
|
| 21 |
*/
|
| 22 |
$.fn.formToArray = function(semantic) {
|
| 23 |
var a = [];
|
| 24 |
if (this.length == 0) return a;
|
| 25 |
|
| 26 |
var form = this[0];
|
| 27 |
var els = semantic ? form.getElementsByTagName('*') : form.elements;
|
| 28 |
if (!els) return a;
|
| 29 |
for(var i=0, max=els.length; i < max; i++) {
|
| 30 |
var el = els[i];
|
| 31 |
var n = el.name;
|
| 32 |
if (!n) continue;
|
| 33 |
|
| 34 |
if (semantic && form.clk && el.type == "image") {
|
| 35 |
// handle image inputs on the fly when semantic == true
|
| 36 |
if(!el.disabled && form.clk == el)
|
| 37 |
a.push({name: n+'.x', value: form.clk_x}, {name: n+'.y', value: form.clk_y});
|
| 38 |
continue;
|
| 39 |
}
|
| 40 |
|
| 41 |
var v = $.fieldValue(el, true);
|
| 42 |
if (v && v.constructor == Array) {
|
| 43 |
for(var j=0, jmax=v.length; j < jmax; j++)
|
| 44 |
a.push({name: n, value: v[j]});
|
| 45 |
}
|
| 46 |
else if (v !== null && typeof v != 'undefined')
|
| 47 |
a.push({name: n, value: v});
|
| 48 |
}
|
| 49 |
|
| 50 |
if (!semantic && form.clk) {
|
| 51 |
// input type=='image' are not found in elements array! handle them here
|
| 52 |
var inputs = form.getElementsByTagName("input");
|
| 53 |
for(var i=0, max=inputs.length; i < max; i++) {
|
| 54 |
var input = inputs[i];
|
| 55 |
var n = input.name;
|
| 56 |
if(n && !input.disabled && input.type == "image" && form.clk == input)
|
| 57 |
a.push({name: n+'.x', value: form.clk_x}, {name: n+'.y', value: form.clk_y});
|
| 58 |
}
|
| 59 |
}
|
| 60 |
return a;
|
| 61 |
};
|
| 62 |
|
| 63 |
/**
|
| 64 |
* Returns the value of the field element.
|
| 65 |
*/
|
| 66 |
$.fieldValue = function(el, successful) {
|
| 67 |
var n = el.name, t = el.type, tag = el.tagName.toLowerCase();
|
| 68 |
if (typeof successful == 'undefined') successful = true;
|
| 69 |
|
| 70 |
if (successful && (!n || el.disabled || t == 'reset' || t == 'button' ||
|
| 71 |
(t == 'checkbox' || t == 'radio') && !el.checked ||
|
| 72 |
(t == 'submit' || t == 'image') && el.form && el.form.clk != el ||
|
| 73 |
tag == 'select' && el.selectedIndex == -1))
|
| 74 |
return null;
|
| 75 |
|
| 76 |
if (tag == 'select') {
|
| 77 |
var index = el.selectedIndex;
|
| 78 |
if (index < 0) return null;
|
| 79 |
var a = [], ops = el.options;
|
| 80 |
var one = (t == 'select-one');
|
| 81 |
var max = (one ? index+1 : ops.length);
|
| 82 |
for(var i=(one ? index : 0); i < max; i++) {
|
| 83 |
var op = ops[i];
|
| 84 |
if (op.selected) {
|
| 85 |
// extra pain for IE...
|
| 86 |
var v = $.browser.msie && !(op.attributes['value'].specified) ? op.text : op.value;
|
| 87 |
if (one) return v;
|
| 88 |
a.push(v);
|
| 89 |
}
|
| 90 |
}
|
| 91 |
return a;
|
| 92 |
}
|
| 93 |
return el.value;
|
| 94 |
};
|
| 95 |
|
| 96 |
})(jQuery);
|