serialize.js 3.1 KB

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