/** * jQuery扩展 序列化表单为对象 */ $.fn.serializeObject = function() { var o = {}; var a = this.serializeArray(); $.each(a, function() { //和下面的方法对比,主要就是这个判断不一样 //这样判断会把空字符串(即空值)的条件也包含进去,下面的方法不会 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; } /** * jQuery扩展 序列化表单为对象(包含空值) */ $.fn.serializeObject2 = function() { var o = {}; var a = this.serializeArray(); $.each(a, function() { //和上面的方法对比,主要就是这个判断不一样 //上面的会把空字符串(即空值)的条件也包含进去,这个不会 if (o[this.name] != null) { 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; }