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
This commit is contained in:
parent
f28f199dc0
commit
f96bf10415
7 changed files with 129 additions and 37 deletions
|
@ -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, "+");
|
||||
}
|
||||
|
||||
});
|
||||
|
|
|
@ -3,7 +3,7 @@ module("ajax");
|
|||
// Safari 3 randomly crashes when running these tests,
|
||||
// but only in the full suite - you can run just the Ajax
|
||||
// tests and they'll pass
|
||||
if ( !jQuery.browser.safari ) {
|
||||
//if ( !jQuery.browser.safari ) {
|
||||
|
||||
test("$.ajax() - success callbacks", function() {
|
||||
expect( 8 );
|
||||
|
@ -163,11 +163,31 @@ test("$.ajax - dataType html", function() {
|
|||
});
|
||||
|
||||
test("serialize()", function() {
|
||||
expect(1);
|
||||
// ignore button, IE takes text content as value, not relevant for this test
|
||||
equals( $(':input').not('button').serialize(),
|
||||
'action=Test&text2=Test&radio1=on&radio2=on&check=on&=on&hidden=&foo%5Bbar%5D=&name=name&=foobar&select1=&select2=3&select3=1&test=&id=',
|
||||
expect(6);
|
||||
|
||||
equals( $('#form').serialize(),
|
||||
"action=Test&radio2=on&check=on&hidden=&foo%5Bbar%5D=&name=name&select1=&select2=3&select3=0&select3=1",
|
||||
'Check form serialization as query string');
|
||||
|
||||
equals( $('#form :input').serialize(),
|
||||
"action=Test&radio2=on&check=on&hidden=&foo%5Bbar%5D=&name=name&select1=&select2=3&select3=0&select3=1",
|
||||
'Check input serialization as query string');
|
||||
|
||||
equals( $('#testForm').serialize(),
|
||||
'T3=%3F%0AZ&H1=x&H2=&PWD=&T1=&T2=YES&My+Name=me&S1=abc&S3=YES&S4=',
|
||||
'Check form serialization as query string');
|
||||
|
||||
equals( $('#testForm :input').serialize(),
|
||||
'T3=%3F%0AZ&H1=x&H2=&PWD=&T1=&T2=YES&My+Name=me&S1=abc&S3=YES&S4=',
|
||||
'Check input serialization as query string');
|
||||
|
||||
equals( $('#form, #testForm').serialize(),
|
||||
"action=Test&radio2=on&check=on&hidden=&foo%5Bbar%5D=&name=name&select1=&select2=3&select3=0&select3=1&T3=%3F%0AZ&H1=x&H2=&PWD=&T1=&T2=YES&My+Name=me&S1=abc&S3=YES&S4=",
|
||||
'Multiple form serialization as query string');
|
||||
|
||||
equals( $('#form, #testForm :input').serialize(),
|
||||
"action=Test&radio2=on&check=on&hidden=&foo%5Bbar%5D=&name=name&select1=&select2=3&select3=0&select3=1&T3=%3F%0AZ&H1=x&H2=&PWD=&T1=&T2=YES&My+Name=me&S1=abc&S3=YES&S4=",
|
||||
'Mixed form/input serialization as query string');
|
||||
});
|
||||
|
||||
test("$.param()", function() {
|
||||
|
@ -618,4 +638,4 @@ test("custom timeout does not set error message when timeout occurs, see #970",
|
|||
|
||||
}
|
||||
|
||||
}
|
||||
//}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue