欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

jQuery把表单元素变为json对象

程序员文章站 2022-11-03 18:35:37
代码如下: (function($){  $.fn.serializeobject=function(){     &n...

代码如下:


(function($){ 
$.fn.serializeobject=function(){ 
           var inputs=$(this).find("input,textarea,select"); 
           var o = {}; 
           $.each(inputs,function(i,n){ 
               switch(n.nodename.touppercase()){ 
                   case "input": 
                       if($(n).is(":checkbox")){ 
                           if($(n).is(":checked")){ 
                               o[n.name]=true; 
                           }else{ 
                               o[n.name]=false; 
                           } 
                       }else if($(n).is(":radio")){ 
                           if($(n).is(":checked")){ 
                               o[n.name]=n.value; 
                           } 
                       }else{ 
                           o[n.name]=n.value;  
                       } 

                       break; 
                   case "textarea": 
                       o[n.name]=$(n).text(); 
                       break; 
                   case "select": 
                       o[n.name]=n.value; 
                       break; 
               } 
           }); 
           return o; 
       } 
})(jquery);