亲宝软件园·资讯

展开

jQuery把表单元素变为json对象

人气:0
复制代码 代码如下:

(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);

加载全部内容

相关教程
猜你喜欢
用户评论