jquery.serializeObject.js 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. //
  2. // Use internal $.serializeArray to get list of form elements which is
  3. // consistent with $.serialize
  4. //
  5. // From version 2.0.0, $.serializeObject will stop converting [name] values
  6. // to camelCase format. This is *consistent* with other serialize methods:
  7. //
  8. // - $.serialize
  9. // - $.serializeArray
  10. //
  11. // If you require camel casing, you can either download version 1.0.4 or map
  12. // them yourself.
  13. //
  14. (function($){
  15. $.fn.serializeObject = function () {
  16. "use strict";
  17. var result = {};
  18. var extend = function (i, element) {
  19. var node = result[element.name];
  20. // If node with same name exists already, need to convert it to an array as it
  21. // is a multi-value field (i.e., checkboxes)
  22. if ('undefined' !== typeof node && node !== null) {
  23. if ($.isArray(node)) {
  24. node.push(element.value);
  25. } else {
  26. result[element.name] = [node, element.value];
  27. }
  28. } else {
  29. result[element.name] = element.value;
  30. }
  31. };
  32. $.each(this.serializeArray(), extend);
  33. return result;
  34. };
  35. })(jQuery);