js将form内的表单序列化为json字符串、数组、对象

  1. 序列化为字符串
    var params1 = $(#myform).serialize();//name=zhangsan&sex=1&age=20


  2. 序列化为数组
    $(#myform).serializeArray();//[Object, Object, Object]


  3. 序列化为对象
    //form表单上数据转成[{name:value},,,,]
    	$.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;    
    	}; 



你可能感兴趣的:(javascript)