将表单序列化成json,将json转换成字符串
//将表单序列化成json 字符串
$.fn.serializeObject = function(){
var obj = {};
var count = 0;
$.each( this.serializeArray(), function(i,o){
var n = o.name, v = o.value;
count++;
obj[n] = obj[n] === undefined ? v
: $.isArray( obj[n] ) ? obj[n].concat( v )
: [ obj[n], v ];
});
obj.nameCounts = count + "";//表单name个数
return JSON.stringify(obj);
}; var objectStr=$("#sysNumberRule-form-addNumberRule").serializeObject(); //转化为json对象 var objectJson=JSON.parse(objectStr);
将表单转换成url
//将表单序列化成json 字符串
$.fn.getFromValueToUrlStr = function(){ var domain = this[0].action;
var count = 0;
var params = '';
$.each( this.serializeArray(), function(i,o){
var n = o.name, v = o.value.replace('&','%23');
count++;
if(v != ''){ params += '&' + n + '=' + v;
} }); if(domain.indexOf('?') ==-1 ){ domain += "?";
} if(domain.charAt(domain.length - 1) == '&'){ domain = domain.substring(0,domain.length - 1);
} return domain + params;
};