Jquery的序列化方法为什么要把空格替换成加号呢?

1.serialize方法:

 

 

 

serialize: function() {
        return jQuery.param(this.serializeArray());
    },

 2.serialize就是对serializeArray方法取的数组进行param操作:

 

 

 

param: function( a ) {
        var s = [ ];

        function add( key, value ){
            s[ s.length ] = encodeURIComponent(key) + '=' + encodeURIComponent(value);
        };

        // If an array was passed in, assume that it is an array
        // of form elements
        if ( jQuery.isArray(a) || a.jquery )
            // Serialize the form elements
            jQuery.each( a, function(){
                add( this.name, 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 ( jQuery.isArray(a[j]) )
                    jQuery.each( a[j], function(){
                        add( j, this );
                    });
                else
                    add( j, jQuery.isFunction(a[j]) ? a[j]() : a[j] );

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

 为什么要替换下呢,为什么要替换成加号呢?return s.join("&").replace(/%20/g, "+");

那是因为URL规范里就是要求空格在query string里被编码为加号吧。
[url]http://en.wikipedia.org/wiki/Query_string#URL_encoding[/url]