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