No Description

serialize.js 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. "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 ( a == null ) {
  58. return "";
  59. }
  60. // If an array was passed in, assume that it is an array of form elements.
  61. if ( Array.isArray( a ) || ( a.jquery && !jQuery.isPlainObject( a ) ) ) {
  62. // Serialize the form elements
  63. jQuery.each( a, function() {
  64. add( this.name, this.value );
  65. } );
  66. } else {
  67. // If traditional, encode the "old" way (the way 1.3.2 or older
  68. // did it), otherwise encode params recursively.
  69. for ( prefix in a ) {
  70. buildParams( prefix, a[ prefix ], traditional, add );
  71. }
  72. }
  73. // Return the resulting serialization
  74. return s.join( "&" );
  75. };
  76. jQuery.fn.extend( {
  77. serialize: function() {
  78. return jQuery.param( this.serializeArray() );
  79. },
  80. serializeArray: function() {
  81. return this.map( function() {
  82. // Can add propHook for "elements" to filter or add form elements
  83. var elements = jQuery.prop( this, "elements" );
  84. return elements ? jQuery.makeArray( elements ) : this;
  85. } )
  86. .filter( function() {
  87. var type = this.type;
  88. // Use .is( ":disabled" ) so that fieldset[disabled] works
  89. return this.name && !jQuery( this ).is( ":disabled" ) &&
  90. rsubmittable.test( this.nodeName ) && !rsubmitterTypes.test( type ) &&
  91. ( this.checked || !rcheckableType.test( type ) );
  92. } )
  93. .map( function( i, elem ) {
  94. var val = jQuery( this ).val();
  95. if ( val == null ) {
  96. return null;
  97. }
  98. if ( Array.isArray( val ) ) {
  99. return jQuery.map( val, function( val ) {
  100. return { name: elem.name, value: val.replace( rCRLF, "\r\n" ) };
  101. } );
  102. }
  103. return { name: elem.name, value: val.replace( rCRLF, "\r\n" ) };
  104. } ).get();
  105. }
  106. } );
  107. return jQuery;
  108. } );