欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

jQuery實現(xiàn)form表單序列化轉(zhuǎn)換為json對象功能示例

 更新時間:2018年05月23日 14:22:30   作者:愛代碼也愛生活  
這篇文章主要介紹了jQuery實現(xiàn)form表單序列化轉(zhuǎn)換為json對象功能,結(jié)合實例形式分析了表單數(shù)據(jù)傳輸中使用serializeJson進(jìn)行序列化操作相關(guān)實現(xiàn)技巧,需要的朋友可以參考下

本文實例講述了jQuery實現(xiàn)form表單序列化轉(zhuǎn)換為json對象功能。分享給大家供大家參考,具體如下:

做web前端開發(fā)時,需要ajax提交大量表單數(shù)據(jù),如果一個個form字段拼接很費(fèi)勁也容易出錯,下面方法可解決這個問題

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <title>jquery form序列化轉(zhuǎn)換為json對象</title>
    <script src="http://cdn.bootcss.com/jquery/3.0.0/jquery.min.js"></script>
    </script>
  </head>
  <body>
    <form action="" name="post_form" id="post_form">
      姓名:<input type="name" name="name" value="王">
      <br/>性別:<input type="radio" name="sex" value="男" checked>男<input type="radio" name="sex" value="女">女
      <br/>愛好:<input type="checkbox" name="loves" value="籃球" >籃球<input type="checkbox" name="loves" value="足球">足球
      <br/>籍貫:<select name="province">
              <option value="上海">上海</option>
              <option value="北京">北京</option>
              <option value="深圳">深圳</option>
            </select>
    </form>
    <div id="result" style="margin-top:20px;width:600px;height:100px;border:1px solid #f00;">
    </div>
    <div>
    <button id="send">發(fā)送</button>
    </div>
    <script>
    //jquery form序列化轉(zhuǎn)換為json對象
    (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])){
              serializeObj[this.name].push(this.value);
            }else{
              serializeObj[this.name]=[serializeObj[this.name],this.value];
            }
          }else{
            serializeObj[this.name]=this.value;
          }
        });
        return serializeObj;
      };
    })(jQuery);
    $(document).ready(function(){
      $("#send").click(function(){
        var post_data=$("#post_form").serializeJson();//表單序列化
        $("#result").html(JSON.stringify(post_data));
      })
    })
    </script>
  </body>
</html>

運(yùn)行效果如下圖所示:

PS:關(guān)于json操作,這里再為大家推薦幾款比較實用的json在線工具供大家參考使用:

在線JSON代碼檢驗、檢驗、美化、格式化工具:
http://tools.jb51.net/code/json

JSON在線格式化工具:
http://tools.jb51.net/code/jsonformat

在線XML/JSON互相轉(zhuǎn)換工具:
http://tools.jb51.net/code/xmljson

json代碼在線格式化/美化/壓縮/編輯/轉(zhuǎn)換工具:
http://tools.jb51.net/code/jsoncodeformat

在線json壓縮/轉(zhuǎn)義工具:
http://tools.jb51.net/code/json_yasuo_trans

更多關(guān)于jQuery相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《jQuery form操作技巧匯總》、《jQuery操作json數(shù)據(jù)技巧匯總》、《jQuery常用插件及用法總結(jié)》、《jQuery擴(kuò)展技巧總結(jié)》、《jQuery表格(table)操作技巧匯總》及《jquery選擇器用法總結(jié)

希望本文所述對大家jQuery程序設(shè)計有所幫助。

相關(guān)文章

最新評論