diff options
author | John Resig <jeresig@gmail.com> | 2007-09-05 17:06:05 +0000 |
---|---|---|
committer | John Resig <jeresig@gmail.com> | 2007-09-05 17:06:05 +0000 |
commit | f96bf1041553775a94c1034c97e253e350217173 (patch) | |
tree | ec325ec45526f3caddac0157f98645ca7aaad64c /src/ajax/ajax.js | |
parent | f28f199dc0a353135ef8b9afa2f3d25c6ffd2c75 (diff) | |
download | jquery-f96bf1041553775a94c1034c97e253e350217173.tar.gz jquery-f96bf1041553775a94c1034c97e253e350217173.zip |
Integration of Mike Alsup's excellent form serialization code. The benefits are as follows:
- New method: .serializeArray()
This returns an array of name/value pairs representing the contents of a form, or individual input elements.
- Enhancement: .serialize()
The results are correct now (as opposed to the mess from before), and allows you to serializes forms directly (rather than just the input elements).
- Enhancement: .val()
This now returns the correct value when dealing wih selects. Additionally, when dealing with multiple selects, it returns an array of values.
Based upon Mike's code:
http://malsup.com/jquery/form/comp/form.js
and test suite:
http://malsup.com/jquery/form/comp/test.html
Diffstat (limited to 'src/ajax/ajax.js')
-rw-r--r-- | src/ajax/ajax.js | 24 |
1 files changed, 20 insertions, 4 deletions
diff --git a/src/ajax/ajax.js b/src/ajax/ajax.js index dd54d0c30..ffe30481d 100644 --- a/src/ajax/ajax.js +++ b/src/ajax/ajax.js @@ -62,9 +62,25 @@ jQuery.fn.extend({ }, serialize: function() { - return jQuery.param( this ); - } - + return jQuery.param(this.serializeArray()); + }, +
serializeArray: function() {
+ return this.map(function(){ + return jQuery.nodeName(this, "form") ? + jQuery.makeArray(this.elements) : this; + }) + .filter(function(){ + return this.name && !this.disabled && + (this.checked || /select|textarea/i.test(this.nodeName) || + /text|hidden|password/i.test(this.type)); + }) + .map(function(i, elem){
var val = jQuery(this).val(); + return val == null ? null : + val.constructor == Array ? + jQuery.map( val, function(i, val){
return {name: elem.name, value: val}; + }) : + {name: elem.name, value: val};
+ }).get();
} }); // Attach a bunch of functions for handling common AJAX events @@ -440,7 +456,7 @@ jQuery.extend({ s.push( encodeURIComponent(j) + "=" + encodeURIComponent( a[j] ) ); // Return the resulting serialization - return s.join("&"); + return s.join("&").replace(/%20/g, "+"); } }); |