jQuery序列化表單為JSON對象

來源:互聯網
上載者:User

標籤:orm   tab   序列   -name   home   inf   table   push   option   

 

 


[html]
 view plain copy 
  1. <form id="myform">  
  2.     <table>  
  3.         <tr>  
  4.             <td>姓名:</td>  
  5.             <td> <input type="text" name="name" /> </td>  
  6.         </tr>  
  7.         <tr>  
  8.             <td>性別:</td>  
  9.             <td>  
  10.                 <input type="radio" name="sex" value="1"> 男  
  11.                 <input type="radio" name="sex" value="0"> 女  
  12.             </td>  
  13.         </tr>  
  14.         <tr>  
  15.             <td>年齡:</td>  
  16.             <td>  
  17.                 <select name="age">  
  18.                     <option value="20">20</option>  
  19.                     <option value="21">21</option>  
  20.                     <option value="22">22</option>  
  21.                 </select>  
  22.             </td>  
  23.         </tr>  
  24.         <tr>  
  25.             <td>愛好</td>  
  26.             <td>  
  27.                 <input type="checkbox" value="basketball" name="hobby">籃球  
  28.                 <input type="checkbox" value="volleyball" name="hobby">排球  
  29.                 <input type="checkbox" value="football" name="hobby">足球  
  30.                 <input type="checkbox" value="earth" name="hobby">地球  
  31.             </td>  
  32.         </tr>  
  33.         <tr>  
  34.             <td colspan="2">  
  35.                 <input type="button" id="ajaxBtn" value="提交" />  
  36.             </td>  
  37.         </tr>  
  38.     </table>  
  39. </form>  
[javascript] view plain copy 
  1. <script type="text/javascript">  
  2.       
  3.     $(function() {  
  4.         $("#ajaxBtn").click(function() {  
  5.                 var params = $("#myform").serializeObject(); //將表單序列化為JSON對象   
  6.                 console.info(params);  
  7.             })  
  8.     })  
  9.   
  10.     $.fn.serializeObject = function() {  
  11.         var o = {};  
  12.         var a = this.serializeArray();  
  13.         $.each(a, function() {  
  14.             if (o[this.name]) {  
  15.                 if (!o[this.name].push) {  
  16.                     o[this.name] = [ o[this.name] ];  
  17.                 }  
  18.                 o[this.name].push(this.value || ‘‘);  
  19.             } else {  
  20.                 o[this.name] = this.value || ‘‘;  
  21.             }  
  22.         });  
  23.         return o;  
  24.     }  
  25. </script>  

 

上述serializeObject方法是將form序列化為JSON對象

jQuery序列化表單為JSON對象

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.