serialize.js 3.1 KB

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