当异步处理数据是jquery很好的提供了serialize与serializeArray方法来获取表单数据 但是有时我们需要的是JSON数据 1. [代码] serializeJSON (function($) {$.fn.serializeJSON = function(options) {var opts = $.exte
但是有时我们需要的是JSON数据
1. [代码]serializeJSON
(function($) { $.fn.serializeJSON = function(options) { var opts = $.extend({}, $.fn.serializeJSON.defaults, options); var toArray = function(value,char){ return value.split(char); } var formData = this.serialize(); var params = toArray(formData,"&"); var resultJSON={}; var connecter = function(name,source,value){ if(!value || value.length<1)return source; var char = opts.connectName[name]?opts.connectName[name]:opts.connect; return source+char+value; } $.each(params,function(){ var param = toArray(this,"="); var attr = param[0]; var val = param[1]; if(resultJSON[attr]){//multi resultJSON[attr] = connecter(attr,resultJSON[attr],val); }else{ resultJSON[attr] = val; } }) return resultJSON; }; $.fn.serializeJSON.defaults = { connectName:{}, connect:"," }; })(jQuery);