hierarchical_select_formtoarray.js 3.2 KB

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