jquery.serialize.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. // Serialize Function
  2. (function($) {
  3. var originalSerializeArray = $.fn.serializeArray;
  4. $.fn.extend({
  5. serializeArray: function () {
  6. var brokenSerialization = originalSerializeArray.apply(this);
  7. var checkboxValues = $(this).find('input[type=checkbox]').map(function () {
  8. return { 'name': this.name, 'value': this.checked };
  9. }).get();
  10. var checkboxKeys = $.map(checkboxValues, function (element) { return element.name; });
  11. var withoutCheckboxes = $.grep(brokenSerialization, function (element) {
  12. return $.inArray(element.name, checkboxKeys) == -1;
  13. });
  14. return $.merge(withoutCheckboxes, checkboxValues);
  15. }
  16. });
  17. $.fn.serializeObject = function()
  18. {
  19. var o = {};
  20. var a = this.serializeArray();
  21. $.each(a, function() {
  22. if (o[this.name] !== undefined) {
  23. if (!o[this.name].push) {
  24. o[this.name] = [o[this.name]];
  25. }
  26. o[this.name].push(this.value || '');
  27. } else {
  28. o[this.name] = this.value || '';
  29. }
  30. });
  31. return o;
  32. };
  33. })(jQuery);