<form method="post" action="#" id="test_form"> 用户名:<input type="text" name="username"/><br> 密 码:<input type="password" name="password"><br> 爱 好:吃饭<input type="checkbox" name="hobby" value="eat" checked/> 睡觉<input type="checkbox" name="hobby" value="sleep"/><br/> 性 别:男 <input type="radio" value="man" name="sex" checked/> 女 <input type="radio" value="woman" name="sex"/><br/> 学 校: <select name="school"> <option value="yangguang">阳光小学</option> <option value="xiwang">希望小学</option> <option value="tiantian">天天小学</option> </select> <br><br><br> <input type="submit" value="提交"/> <input type="reset" value="重置" /> <br> <br> <br> <input type="button" value="点我序列化为url" id="serializeUrl"/> <input type="button" value="点我序列化为json" id="serializeJson"/> </form> 1.var str = $("#test_form").serialize();//返回字符串 输出:username=&password=&hobby=eat&sex=man&school=yangguang 2.var jsonData = $("#test_form").serializeArray();//返回json对象 输出:[ {"name":"username","value":""}, {"name":"password","value":""}, {"name":"hobby","value":"eat"}, {"name":"sex","value":"man"}, {"name":"school","value":"yangguang"} ] 3.使用serializeJson(),并不是jquery自带的函数, (function($){ $.fn.serializeJson=function(){ var serializeObj={}; var array=this.serializeArray(); var str=this.serialize(); $(array).each(function(){ if(serializeObj[this.name]){ if($.isArray(serializeObj[this.name])&&this .value!=""){ serializeObj[this.name].push(this.value); }else { if(this .value!=""){ serializeObj[this.name]=[serializeObj[this.name],this.value]; } } }else{ if(this .value!=""){ serializeObj[this.name]=this.value; } } }); return serializeObj; }; })(jQuery); 使用:var data = $("#test_form").serializeJson();//返回json对象 输出:序列化为json格式为:{"hobby":"eat","sex":"man","school":"yangguang"} ———————————————— 版权声明:本文为CSDN博主「tyjlearning」的原创文章,遵循CC 4.0 by-sa版权协议,转载请附上原文出处链接及本声明。 原文链接:https://blog.csdn.net/tyjlearning/article/details/81315621