六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 34|回复: 0

JQuery插件应用(二)

[复制链接]

升级  72%

8

主题

8

主题

8

主题

童生

Rank: 1

积分
36
 楼主| 发表于 2013-1-23 02:18:51 | 显示全部楼层 |阅读模式
今天说说jQuery.Form,jQuery.Form的官方网站是http://jquery.malsup.com/form/
提到jQuery.Form首先得说说Ajax数据传递乱码问题,Ajax数据传递编码为UTF-8,无论你设置了程序级编码还是页面级编码,Jquery都会使用UTF-8的编码方式传递过去,如果你采用GBK,GB2312方式会。简单起见需要从JQuery源码着手。

1、jQuery 1.2.6 乱码解决办法
论坛里可以找到http://www.iteye.com/topic/339793,具体如下:
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, "+");    }
2、jQuery 1.3.2 乱码解决办法
param: function( a ) {var s = [ ]; function add( key, value ){s[ s.length ] = encodeURIComponent(key) + '=' + encodeURIComponent(encodeURIComponent(value));};
修改完后再程序里,比如struts的action里用java.net.URLDecoder.decode(param, "UTF-8");进行转码。

书归正传,上一段jquery.form代码:
<s:form id="searchForm" theme="simple" action="monitorItemListView" namespace="/monitorinfo">名称:<s:textfield name="monItemsName" /><s:submit value="提交" /></label></s:form>
$(document).ready(function() {   var options = {           success : function(msg){//alert(msg);$("#item_list").html(msg);          },  // post-submit callback           type : "post",        // 'get' or 'post', override for form's 'method' attribute           resetForm: true                  //timeout:   3000       };          // bind form using 'ajaxForm'       $('#searchForm').ajaxForm(options);   });
代码非常简单,一看就明白了,有啥米问题可以留言讨论。最后奉上修改后的支持GBK的jquery和Jquery混淆工具。
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表