日期:2014-05-16  浏览次数:20430 次

让jQuery.form.js支持中文
$.fn.param=function( a ) {     
   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(){
                 s.push( encodeURIComponent(this.name) + "=" + encodeURIComponent(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(){
                         s.push( encodeURIComponent(j) + "=" + encodeURIComponent(encodeURIComponent( this )) );
                     });
                 else
                     s.push( encodeURIComponent(j) + "=" + encodeURIComponent(encodeURIComponent( jQuery.isFunction(a[j]) ? a[j]() : a[j] )) );

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

?ajax中的提交,都是以UTF-8的编码方式提交,如果我们用的GBK的话,就要对编码做转换才可以,我的项目中用到了jquery.form.js 做提交,只有一个页面在用,其它页面的ajax提交我都做了encodeURIComponent转换,所以不能直接改jquery.js文件,不过要是改jquery.js的话,是这样改的。

?

我现在只想改一下jquery.form.js文件,就可以这样改,新增以下方法

?

// Serialize an array of form elements or a set of
    // key/values into a query string
    param: function( a ) {
        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(){
                s.push( encodeURIComponent(this.name) + "=" + encodeURIComponent(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 && a.constructor == Array )
                    jQuery.each( a, function(){
                        s.push( encodeURIComponent(j) + "=" + encodeURIComponent(encodeURIComponent( this )) );
                    });
                else
                    s.push( encodeURIComponent(j) + "=" + encodeURIComponent(encodeURIComponent( jQuery.isFunction(a) ? a() : a )) );

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

?

?然后对文件中用到$.param 的地方改成 this.param就可以。

?

java后台在做编码转换

URLDecoder.decode(tform.getSapppro(),"utf-8")

?

目前这样处理我测试没有什么问题,以后还是用utf-8编码吧,不然太麻烦了。

?

上下两个代码块插入反了,修改jquery.js时,用第二个;修改jquery.form.js时用第一个