Bug Tracker

Opened 12 years ago

Closed 11 years ago

#1814 closed enhancement (wontfix)

$.param add parameter 'clean' to filter out empty elements if desired

Reported by: yereth Owned by: flesler
Priority: minor Milestone: 1.3
Component: ajax Version: 1.2.1
Keywords: $.param, clean serialize, filter serialize Cc:
Blocked by: Blocking:

Description

I added the following code to my jQuery 1.2.1 version, because for me it's very useful to be able to filter a serialized parameter list to exclude empty values. This is the change (mind the new 'clean' parameter:

	// Serialize an array of form elements or a set of
	// key/values into a query string
	param: function( a, clean ) {
		var s = [];

		// If an array was passed in, assume that it is an array
		// of form elements
		if ( a.constructor == Array || a.jquery )
			// Serialize the form elements
			jQuery.each( a, function(){
				if (!clean || $.trim(this.value)) s.push( encodeURIComponent(this.name) + "=" + encodeURIComponent( this.value ) );
			});

		// Otherwise, assume that it's an object of key/value pairs
		else
			// Serialize the key/values
			for ( var j in a )
				// If the value is an array then the key names need to be repeated
				if ( a[j] && a[j].constructor == Array )
					jQuery.each( a[j], function(){
						if (!clean || $.trim(this)) s.push( encodeURIComponent(j) + "=" + encodeURIComponent( this ) );
					});
				else
					'''if (!clean || $.trim(a[j]))''' s.push( encodeURIComponent(j) + "=" + encodeURIComponent( a[j] ) );

		// Return the resulting serialization
		return s.join("&").replace(/%20/g, "+");
	}

Is it an idea to include in the core?

Change History (4)

comment:1 Changed 12 years ago by john

Component: coreajax

comment:2 Changed 12 years ago by flesler

Owner: set to flesler
Status: newassigned

comment:3 Changed 12 years ago by flesler

Milestone: 1.2.21.3
need: ReviewTest Case

Could you provide a situation where filtering these values makes a big change ?

comment:4 Changed 11 years ago by flesler

Resolution: wontfix
Status: assignedclosed
Note: See TracTickets for help on using tickets.