表单序列化得常用方法
方法1:serialize(): 就是把表单信息序列化成一个字符串 (认为最常用 的方法) <html> <head> <script type="text/javascript" src="/jquery/jquery.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("button").click(function(){ $("div").text($("form").serialize()); }); }); </script> </head> <body> <form action=""> First name: <input type="text" name="FirstName" value="Bill" /><br /> Last name: <input type="text" name="LastName" value="Gates" /><br /> </form> <button>序列化表单值</button> <div></div> </body> </html> 方法2:serializeArray() 它返回的是一个json 对象 而不是一个字符串 <html> <head> <script type="text/javascript" src="/jquery/jquery.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("button").click(function(){ x=$("form").serializeArray(); var x = JSON.stringify(x) document.write(x) }); }); </script> </head> <body> <form action=""> First name: <input type="text" name="FirstName" value="Bill" /><br /> Last name: <input type="text" name="LastName" value="Gates" /><br /> </form> <button>序列化表单值</button> <div id="results"></div> </body> </html> 方法3:是自己自定义的serializeJson()方法: <form> <input type="text" name="username" /> <input type="text" name="password" /> </form> <script> jQuery.prototype.serializeObject=function(){ var obj=new Object(); $.each(this.serializeArray(),function(index,param){ if(!(param.name in obj)){ obj[param.name]=param.value; } }); return obj; }; </script>