serialize.js (3236B)
1 define( [ 2 "./core", 3 "./core/toType", 4 "./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 ( a == null ) { 74 return ""; 75 } 76 77 // If an array was passed in, assume that it is an array of form elements. 78 if ( Array.isArray( a ) || ( a.jquery && !jQuery.isPlainObject( a ) ) ) { 79 80 // Serialize the form elements 81 jQuery.each( a, function() { 82 add( this.name, this.value ); 83 } ); 84 85 } else { 86 87 // If traditional, encode the "old" way (the way 1.3.2 or older 88 // did it), otherwise encode params recursively. 89 for ( prefix in a ) { 90 buildParams( prefix, a[ prefix ], traditional, add ); 91 } 92 } 93 94 // Return the resulting serialization 95 return s.join( "&" ); 96 }; 97 98 jQuery.fn.extend( { 99 serialize: function() { 100 return jQuery.param( this.serializeArray() ); 101 }, 102 serializeArray: function() { 103 return this.map( function() { 104 105 // Can add propHook for "elements" to filter or add form elements 106 var elements = jQuery.prop( this, "elements" ); 107 return elements ? jQuery.makeArray( elements ) : this; 108 } ).filter( function() { 109 var type = this.type; 110 111 // Use .is( ":disabled" ) so that fieldset[disabled] works 112 return this.name && !jQuery( this ).is( ":disabled" ) && 113 rsubmittable.test( this.nodeName ) && !rsubmitterTypes.test( type ) && 114 ( this.checked || !rcheckableType.test( type ) ); 115 } ).map( function( _i, elem ) { 116 var val = jQuery( this ).val(); 117 118 if ( val == null ) { 119 return null; 120 } 121 122 if ( Array.isArray( val ) ) { 123 return jQuery.map( val, function( val ) { 124 return { name: elem.name, value: val.replace( rCRLF, "\r\n" ) }; 125 } ); 126 } 127 128 return { name: elem.name, value: val.replace( rCRLF, "\r\n" ) }; 129 } ).get(); 130 } 131 } ); 132 133 return jQuery; 134 } );