0
|
1 define( [
|
|
2 "./core",
|
|
3 "./core/toType",
|
|
4 "./manipulation/var/rcheckableType",
|
|
5 "./var/isFunction",
|
|
6 "./core/init",
|
|
7 "./traversing", // filter
|
|
8 "./attributes/prop"
|
|
9 ], function( jQuery, toType, rcheckableType, isFunction ) {
|
|
10
|
|
11 "use strict";
|
|
12
|
|
13 var
|
|
14 rbracket = /\[\]$/,
|
|
15 rCRLF = /\r?\n/g,
|
|
16 rsubmitterTypes = /^(?:submit|button|image|reset|file)$/i,
|
|
17 rsubmittable = /^(?:input|select|textarea|keygen)/i;
|
|
18
|
|
19 function buildParams( prefix, obj, traditional, add ) {
|
|
20 var name;
|
|
21
|
|
22 if ( Array.isArray( obj ) ) {
|
|
23
|
|
24 // Serialize array item.
|
|
25 jQuery.each( obj, function( i, v ) {
|
|
26 if ( traditional || rbracket.test( prefix ) ) {
|
|
27
|
|
28 // Treat each array item as a scalar.
|
|
29 add( prefix, v );
|
|
30
|
|
31 } else {
|
|
32
|
|
33 // Item is non-scalar (array or object), encode its numeric index.
|
|
34 buildParams(
|
|
35 prefix + "[" + ( typeof v === "object" && v != null ? i : "" ) + "]",
|
|
36 v,
|
|
37 traditional,
|
|
38 add
|
|
39 );
|
|
40 }
|
|
41 } );
|
|
42
|
|
43 } else if ( !traditional && toType( obj ) === "object" ) {
|
|
44
|
|
45 // Serialize object item.
|
|
46 for ( name in obj ) {
|
|
47 buildParams( prefix + "[" + name + "]", obj[ name ], traditional, add );
|
|
48 }
|
|
49
|
|
50 } else {
|
|
51
|
|
52 // Serialize scalar item.
|
|
53 add( prefix, obj );
|
|
54 }
|
|
55 }
|
|
56
|
|
57 // Serialize an array of form elements or a set of
|
|
58 // key/values into a query string
|
|
59 jQuery.param = function( a, traditional ) {
|
|
60 var prefix,
|
|
61 s = [],
|
|
62 add = function( key, valueOrFunction ) {
|
|
63
|
|
64 // If value is a function, invoke it and use its return value
|
|
65 var value = isFunction( valueOrFunction ) ?
|
|
66 valueOrFunction() :
|
|
67 valueOrFunction;
|
|
68
|
|
69 s[ s.length ] = encodeURIComponent( key ) + "=" +
|
|
70 encodeURIComponent( value == null ? "" : value );
|
|
71 };
|
|
72
|
|
73 // If an array was passed in, assume that it is an array of form elements.
|
|
74 if ( Array.isArray( a ) || ( a.jquery && !jQuery.isPlainObject( a ) ) ) {
|
|
75
|
|
76 // Serialize the form elements
|
|
77 jQuery.each( a, function() {
|
|
78 add( this.name, this.value );
|
|
79 } );
|
|
80
|
|
81 } else {
|
|
82
|
|
83 // If traditional, encode the "old" way (the way 1.3.2 or older
|
|
84 // did it), otherwise encode params recursively.
|
|
85 for ( prefix in a ) {
|
|
86 buildParams( prefix, a[ prefix ], traditional, add );
|
|
87 }
|
|
88 }
|
|
89
|
|
90 // Return the resulting serialization
|
|
91 return s.join( "&" );
|
|
92 };
|
|
93
|
|
94 jQuery.fn.extend( {
|
|
95 serialize: function() {
|
|
96 return jQuery.param( this.serializeArray() );
|
|
97 },
|
|
98 serializeArray: function() {
|
|
99 return this.map( function() {
|
|
100
|
|
101 // Can add propHook for "elements" to filter or add form elements
|
|
102 var elements = jQuery.prop( this, "elements" );
|
|
103 return elements ? jQuery.makeArray( elements ) : this;
|
|
104 } )
|
|
105 .filter( function() {
|
|
106 var type = this.type;
|
|
107
|
|
108 // Use .is( ":disabled" ) so that fieldset[disabled] works
|
|
109 return this.name && !jQuery( this ).is( ":disabled" ) &&
|
|
110 rsubmittable.test( this.nodeName ) && !rsubmitterTypes.test( type ) &&
|
|
111 ( this.checked || !rcheckableType.test( type ) );
|
|
112 } )
|
|
113 .map( function( i, elem ) {
|
|
114 var val = jQuery( this ).val();
|
|
115
|
|
116 if ( val == null ) {
|
|
117 return null;
|
|
118 }
|
|
119
|
|
120 if ( Array.isArray( val ) ) {
|
|
121 return jQuery.map( val, function( val ) {
|
|
122 return { name: elem.name, value: val.replace( rCRLF, "\r\n" ) };
|
|
123 } );
|
|
124 }
|
|
125
|
|
126 return { name: elem.name, value: val.replace( rCRLF, "\r\n" ) };
|
|
127 } ).get();
|
|
128 }
|
|
129 } );
|
|
130
|
|
131 return jQuery;
|
|
132 } );
|