分享

form 转json最佳示例

 大芬油画 2016-01-03
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="application/javascript" src="js/jquery-2.0.3.js"></script>
<title>无标题文档</title>
<script type="application/javascript">
$.fn.serializeObject = function() 
   var o = {}; 
   var a = this.serializeArray(); 
   $.each(a, function() { 
       if (o[this.name]) { 
           if (!o[this.name].push) { 
               o[this.name] = [o[this.name]]; 
           
           o[this.name].push(this.value || ''); 
       } else
           o[this.name] = this.value || ''
       
   }); 
   return o; 
};
function onClik(){
        //var data = $("#form1").serializeArray(); //自动将form表单封装成json
        //alert(JSON.stringify(data));
        var jsonuserinfo = $('#form1').serializeObject();
        alert(JSON.stringify(jsonuserinfo));
}
</script>
<form id="form1" name="form1" method="post" action="">
  <p>进货人 :
    <label for="name"></label>
    <input name="name" id="name" type="text">
  </p>
  <p>性别:
    <label for="sex"></label>
    <select name="sex" size="1" id="sex">
      <option value="1">男</option>
      <option value="2">女</option>
    </select>
  </p>
  <table border="1" width="708">
    <tbody><tr>
      <td width="185">商品名</td>
      <td width="205">商品数量</td>
      <td width="296">商品价格</td>
    </tr>
    <tr>
      <td><label for="pro_name"></label>
        <input name="pro_name" id="pro_name" type="text"></td>
      <td><label for="pro_num"></label>
        <input name="pro_num" id="pro_num" type="text"></td>
      <td><label for="pro_price"></label>
        <input name="pro_price" id="pro_price" type="text"></td>
    </tr>
    <tr>
      <td><input name="pro_name2" id="pro_name2" type="text"></td>
      <td><input name="pro_num2" id="pro_num2" type="text"></td>
      <td><input name="pro_price2" id="pro_price2" type="text"></td>
    </tr>
  </tbody></table>
  <p> </p>
  <input name="submit" onclick="onClik();" value="提交" type="button">
</form>

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约