/** * @author kyle florence * @website https://github.com/kflorence/jquery-deserialize/ * @version 1.2.1 * * dual licensed under the mit and gplv2 licenses. */ (function( jquery, undefined ) { var push = array.prototype.push, rcheck = /^(?:radio|checkbox)$/i, rplus = /\+/g, rselect = /^(?:option|select-one|select-multiple)$/i, rvalue = /^(?:button|color|date|datetime|datetime-local|email|hidden|month|number|password|range|reset|search|submit|tel|text|textarea|time|url|week)$/i; function getelements( elements ) { return elements.map(function() { return this.elements ? jquery.makearray( this.elements ) : this; }).filter( ":input:not(:disabled)" ).get(); } function getelementsbyname( elements ) { var current, elementsbyname = {}; jquery.each( elements, function( i, element ) { current = elementsbyname[ element.name ]; elementsbyname[ element.name ] = current === undefined ? element : ( jquery.isarray( current ) ? current.concat( element ) : [ current, element ] ); }); return elementsbyname; } jquery.fn.deserialize = function( data, options ) { var i, length, elements = getelements( this ), normalized = []; if ( !data || !elements.length ) { return this; } if ( jquery.isarray( data ) ) { normalized = data; } else if ( jquery.isplainobject( data ) ) { var key, value; for ( key in data ) { jquery.isarray( value = data[ key ] ) ? push.apply( normalized, jquery.map( value, function( v ) { return { name: key, value: v }; })) : push.call( normalized, { name: key, value: value } ); } } else if ( typeof data === "string" ) { var parts; data = data.split( "&" ); for ( i = 0, length = data.length; i < length; i++ ) { parts = data[ i ].split( "=" ); push.call( normalized, { name: decodeuricomponent( parts[ 0 ].replace( rplus, "%20" ) ), value: decodeuricomponent( parts[ 1 ].replace( rplus, "%20" ) ) }); } } if ( !( length = normalized.length ) ) { return this; } var current, element, j, len, name, property, type, value, change = jquery.noop, complete = jquery.noop, names = {}; options = options || {}; elements = getelementsbyname( elements ); // backwards compatible with old arguments: data, callback if ( jquery.isfunction( options ) ) { complete = options; } else { change = jquery.isfunction( options.change ) ? options.change : change; complete = jquery.isfunction( options.complete ) ? options.complete : complete; } for ( i = 0; i < length; i++ ) { current = normalized[ i ]; name = current.name; value = current.value; if ( !( element = elements[ name ] ) ) { continue; } type = ( len = element.length ) ? element[ 0 ] : element; type = ( type.type || type.nodename ).tolowercase(); property = null; if ( rvalue.test( type ) ) { if ( len ) { j = names[ name ]; element = element[ names[ name ] = ( j == undefined ) ? 0 : ++j ]; } change.call( element, ( element.value = value ) ); } else if ( rcheck.test( type ) ) { property = "checked"; } else if ( rselect.test( type ) ) { property = "selected"; } if ( property ) { if ( !len ) { element = [ element ]; len = 1; } for ( j = 0; j < len; j++ ) { current = element[ j ]; if ( current.value == value ) { change.call( current, ( current[ property ] = true ) && value ); } } } } complete.call( this ); return this; }; })( jquery );