Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

126 lines
3.2KB

  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(
  24. prefix + "[" + ( typeof v === "object" && v != null ? i : "" ) + "]",
  25. v,
  26. traditional,
  27. add
  28. );
  29. }
  30. } );
  31. } else if ( !traditional && jQuery.type( obj ) === "object" ) {
  32. // Serialize object item.
  33. for ( name in obj ) {
  34. buildParams( prefix + "[" + name + "]", obj[ name ], traditional, add );
  35. }
  36. } else {
  37. // Serialize scalar item.
  38. add( prefix, obj );
  39. }
  40. }
  41. // Serialize an array of form elements or a set of
  42. // key/values into a query string
  43. jQuery.param = function( a, traditional ) {
  44. var prefix,
  45. s = [],
  46. add = function( key, value ) {
  47. // If value is a function, invoke it and return its value
  48. value = jQuery.isFunction( value ) ? value() : ( value == null ? "" : value );
  49. s[ s.length ] = encodeURIComponent( key ) + "=" + encodeURIComponent( value );
  50. };
  51. // Set traditional to true for jQuery <= 1.3.2 behavior.
  52. if ( traditional === undefined ) {
  53. traditional = jQuery.ajaxSettings && jQuery.ajaxSettings.traditional;
  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( "&" ).replace( r20, "+" );
  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. } );