将hmtl元素包括其下子元素转换为Json对象 /******************************************* @example var json = $(formSelector).toJson() ; ******************************************/jQuery.fn.toJson = function(beforeExtend,afterExten
/******************************************* @example var json = $(formSelector).toJson() ; ******************************************/ jQuery.fn.toJson = function(beforeExtend,afterExtend,params) { var me = jQuery(this) ; beforeExtend = beforeExtend||{} ; afterExtend = afterExtend||{} ; params = params||{} ; var a = {}; //text hidden password me.find("input[type=text],input[type=hidden],input[type=password]").each( function(){ _add(this.name||this.id,this.value) ; } ) ; me.find("textarea").each( function(){ _add(this.name||this.id,this.value) ; } ) ; //radio me.find("input[type=radio]").filter(":checked").each( function(){ _add(this.name||this.id,this.value) ; } ) ; //checkbox var temp_cb = "" ; me.find("input[type=checkbox]").filter(":checked").each( function(){ if (temp_cb.indexOf(this.name ) == -1) { temp_cb += (this.name) + ","; } } ) ; jQuery( temp_cb.split(",") ).each( function(){ var tempValue = [] ; jQuery("input[name='" + this + "']:checked").each(function(i) { tempValue.push( this.value ) ; }); _add(this ,tempValue.join(",")) ; } ) ; //select me.find('select').each( function(){ var multi = $(this).attr('multiple') ; var val = [] ; jQuery(this).find('option:selected').each(function(){ if(this.value)val.push( this.value ) ; }); if( val.length == 0 ){ val.push(this.value||"") ; } if(multi && params.mulSelectSplit ){ _add(this.name||this.id,"'"+val.join("','")+"'") ; }else{ _add(this.name||this.id,val.join(',')) ; } } ) ; return $.extend(beforeExtend , a , afterExtend) ; function _add(key,value){ if(key == "__ValidatorRules") return ; if(!key || !jQuery.trim(key)) return ; value = value||'' ; a[key] = value ; } } jQuery.extend( { /** * @see 将javascript数据类型转换为json字符串 * @param 待转换对象,支持object,array,string,function,number,boolean,regexp * @return 返回json字符串 */ ChangedToJSON: function (object) { var type = typeof object; if ('object' == type) { if (Array == object.constructor) type = 'array'; else if (RegExp == object.constructor) type = 'regexp'; else type = 'object'; } switch (type) { case 'undefined': case 'unknown': return; break; case 'function': case 'boolean': case 'regexp': return object.toString(); break; case 'number': return isFinite(object) ? object.toString() : 'null'; break; case 'string': return '"' + object.replace('/(//|/")/g', '"//$1"').replace('//n|/r|/t/g', function () { var a = arguments[0]; return (a == '/n') ? '//n' : (a == '/r') ? '//r' : (a == '/t') ? '//t' : "" }) + '"'; break; case 'object': if (object === null) return 'null'; var results = []; for (var property in object) { var value = jQuery.ChangedToJSON(object[property]); if (value !== undefined) results.push(jQuery.ChangedToJSON(property) + ':' + value); } return '{' + results.join(',') + '}'; break; case 'array': var results = []; for (var i = 0; i < object.length; i++) { var value = jQuery.ChangedToJSON(object[i]); if (value !== undefined) results.push(value); } return '[' + results.join(',') + ']'; break; } } });