eightHundreds
6/27/2017 - 7:07 AM

serializeFormToObject.js

JQuery表单序列化

(function($) {
//将dom下的所有表单元素序列化
	$.fn.serializeAnything = function() {

		var toReturn	= [];
		var els 		= $(this).find(':input').get();

		$.each(els, function() {
			if (this.name && !this.disabled && (this.checked || /select|textarea/i.test(this.nodeName) || /text|hidden|password/i.test(this.type))) {
				var val = $(this).val();
				toReturn.push( encodeURIComponent(this.name) + "=" + encodeURIComponent( val ) );
			}
		});

		return toReturn.join("&").replace(/%20/g, "+");

	}

})(jQuery);
 $.fn.serializeFormToObject = function() {
        //serialize to array
        var data = $(this).serializeArray();

        //add also disabled items
        $(':disabled[name]', this)
            .each(function() {
                data.push({ name: this.name, value: $(this).val() });
            });

        //map to object
        var obj = {};
        data.map(function(x) { obj[x.name] = x.value; });

        return obj;
};