Skip to main content

Bug Tracker

Side navigation

#1814 closed enhancement (wontfix)

Opened October 17, 2007 03:33PM UTC

Closed July 28, 2008 04:03PM UTC

$.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?

Attachments (0)
Change History (4)

Changed December 11, 2007 05:06PM UTC by john comment:1

component: coreajax

Changed May 15, 2008 09:08PM UTC by flesler comment:2

owner: → flesler
status: newassigned

Changed May 27, 2008 10:49PM UTC by flesler comment:3

milestone: 1.2.21.3
need: ReviewTest Case

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

Changed July 28, 2008 04:03PM UTC by flesler comment:4

resolution: → wontfix
status: assignedclosed