最近項目用到JqGrid。在查詢的時候需要把Form表單序列化添加到Jqrid的postData中。再次做個備份,也希望可以幫助遇到此問題的戰友~~
/**将表單序列化成 json,并清除空值項**/
$.fn.serializeObject = function() {
var o = {};
var a = this.serializeArray();
$.each(a, function() {
if(this.value != -1 && this.value.length > 0){
if (o[this.name]) {
if (!o[this.name].push) {
o[this.name] = [ o[this.name] ];
}
o[this.name].push(this.value || '');
} else {
o[this.name] = this.value || '';
}
}
});
return o;
}
用法:
在JqGrid的配置中添加此配置
serializeGridData:function(postData){ //序列化參數
return $.extend($("#searchForm").serializeObject(),postData);
}