1
0

serialize.js 3.1 KB

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