//Author:rikugun  
jQuery.fn.extend({  
   fillForm:function(options){  
       var settings = jQuery.extend({  
           prefix:'' //表单项name前缀  
           ,data:{} //数据默认值  
       },options);  
   this.each(function(){  
       var that =jQuery(this);  
       if(jQuery.inArray(that.attr('tagName').toLowerCase(),['input','select','radio','textarea'])>-1){  
           that.val(settings.data[that.attr('name')]);  
       }else{  
           for(var item in settings.data){  
               try{  
                   that.find('[name=\''+settings.prefix+item+'\']').val(settings.data[item]);  
               }catch(e){  
                   if(window.console){  
                       console.error('未能获取元素:'+settings.prefix+item);  
                   }  
               }  
           }  
       }  
   });  
   }  
   ,getForm:function(options){  
       var settings = jQuery.extend({  
           prefix:'' //表单项name前缀  
           ,data:{} //数据默认值  
       },options);  
       var ret =[];  
       var forms = this;  
       if(this.attr('tagName').toLowerCase()!=='form'){  
           forms = this.find('form');  
       }  
       forms.each(function(){  
           var that =jQuery(this);  
           var o ={};  
           that.find('input,select,radio,textarea').each(function(){  
               var el = jQuery(this);  
               var fieldFullName = el.attr('name');  
               var fieldName =fieldFullName.replace(settings.prefix,'');  
               if(settings.data[fieldName]){  
                   o[fieldName] =settings.data[fieldName];  
                   return;  
               }  
               if(settings.prefix && fieldFullName.indexOf(settings.prefix)<0 ){  
                   //不符合前缀或已设置默认值  
                   return;  
               }  
               o[fieldName] = el.val();  
           });  
           ret.push(o);  
       });  
       if(ret.length === 1)return ret[0];  
       return ret;  
   }  
});  

//扩展jQuery对json字符串的转换
jQuery.extend({
   /** * @see 将json字符串转换为对象 * @param json字符串 * @return 返回object,array,string等对象 */
   evalJSON: function(strJson) {
     return eval("(" + strJson + ")");
   }
});
jQuery.extend({
   /** * @see 将javascript数据类型转换为json字符串 * @param 待转换对象,支持object,array,string,function,number,boolean,regexp * @return 返回json字符串 */
   toJSON: 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.toJSON(object[property]);
         if (value !== undefined) results.push(jQuery.toJSON(property) + ':' + value);
       }
       return '{' + results.join(',') + '}';
       break;
     case 'array':
       var results = [];
       for (var i = 0; i < object.length; i++) {
         var value = jQuery.toJSON(object[i]);
         if (value !== undefined) results.push(value);
       }
       return '[' + results.join(',') + ']';
       break;
     }
   }
});