form 转json最佳示例

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<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>
</head>

<body>
<form id="form1" name="form1" method="post" action="">
  <p>进货人 :
    <label for="name"></label>
    <input type="text" name="name" id="name" />
  </p>
  <p>性别:
    <label for="sex"></label>
    <select name="sex" size="1" id="sex">
      <option value="1">男</option>
      <option value="2">女</option>
    </select>
  </p>
  <table width="708" border="1">
    <tr>
      <td width="185">商品名</td>
      <td width="205">商品数量</td>
      <td width="296">商品价格</td>
    </tr>
    <tr>
      <td><label for="pro_name"></label>
        <input type="text" name="pro_name" id="pro_name" /></td>
      <td><label for="pro_num"></label>
        <input type="text" name="pro_num" id="pro_num" /></td>
      <td><label for="pro_price"></label>
        <input type="text" name="pro_price" id="pro_price" /></td>
    </tr>
    <tr>
      <td><input type="text" name="pro_name2" id="pro_name2" /></td>
      <td><input type="text" name="pro_num2" id="pro_num2" /></td>
      <td><input type="text" name="pro_price2" id="pro_price2" /></td>
    </tr>
  </table>
  <p>&nbsp;</p>
  <input type="button" name="submit" onclick="onClik();" value="提交"/>
</form>
</body>
</html>

时间: 2024-08-30 06:20:45

form 转json最佳示例的相关文章

谈CSS的标准和最佳示例-Div+CSS教程

本文的讲解将帮助你了解网站和CSS文件,还将帮助你们了解CSS文件中的内容.索引部分知识一个格式化了的CSS注释段落,CSS的标准和最佳示例. CSS是用来定义网站的用户界面或将显示和内容区分开来的. 由于CSS被广泛地使用在几乎所有网站上,所以让我们花些时间来创建样式表并确保它符合良好的标准.下面的技巧会极大地在开发过程中帮助CSS初学者. 索引 本处的定义将帮助你和其他开发者了解网站和CSS文件,还将帮助你们了解CSS文件中的内容.索引部分知识一个格式化了的CSS注释段落. 1给出CSS文件

PHP自定义函数格式化json数据示例_php技巧

本文实例讲述了PHP自定义函数格式化json数据的方法.分享给大家供大家参考,具体如下: <?php /** * Formats a JSON string for pretty printing * * @param string $json The JSON to make pretty * @param bool $html Insert nonbreaking spaces and <br />s for tabs and linebreaks * @return string

对象-form 转json的问题··········

问题描述 form 转json的问题·········· 下面是将表单数据转换成json对象其中的 o[this.name] 老不理解是什么意思?求大神指教!var 0={};表示声明一个json对象o[this.name]是什么意思啊?这是什么写法啊? // 将表单数据转换成json对象 $.fn.serializeObject = function() { var o = {}; var a = this.serializeArray(); $.each(a function() { if

php格式化json函数示例代码_php技巧

本文讲述了php格式化json函数的示例代码.分享给大家供大家参考,具体如下: <?php $arr = array("ret"=>0,"data"=>array('a' => 1, 'b' => '2', 'c' => 3, 'd' => 4, 'e' => 5)); $json = json_encode($arr); /** * Formats a JSON string for pretty printing

实用ExtJS教程100例-011:ExtJS Form 使用JSON数据赋值和取值

上一节中我们演示了ExtJS Form的异步加载和提交数据,本节中我们将演示如何使用JSON数据为ExtJS Form中的字段赋值和取值. 系列ExtJS教程持续更新中,点击查看>>最新ExtJS教程目录 使用JSON数据为字段赋值 var formCmp = this.up("form"); var form = formCmp.getForm(); var userValues = { UserName: "Qi Fei", Email: "

jquery提交form表单简单示例分享

 这篇文章主要介绍了jquery提交form表单示例,需要的朋友可以参考下   代码如下: $.ajax({ url : 'deliveryWarrant/update.do', data : $('#myform').serialize(), type : "POST", success : function(data) { var res = eval('(' + data + ')'); if (res && res.success == true) {  ale

ajax后台处理返回json值示例代码_AJAX相关

复制代码 代码如下: public ActionForward xsearch(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { String parentId = request.getParameter("parentId"); String supplier = request.getParamet

ajax后台处理返回json值示例代码

复制代码 代码如下: public ActionForward xsearch(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { String parentId = request.getParameter("parentId"); String supplier = request.getParamet

jquery解析JSON数据示例代码

 作为jquery异步请求的传输对象,jquery请求后返回的结果是json对象,这里考虑的都是服务器返回JSON形式的字符串的形式 这里可以找到json.js的代码,后面还需要formutil.js的代码及MD5.js    用jquery解析JSON数据的方法,作为jquery异步请求的传输对象,jquery请求后返回的结果是json对象,这里考虑的都是服务器返回JSON形式的字符串的形式,对于利用JSONObject等插件封装的JSON对象,与此亦是大同小异,这里不再做说明.  这里首先给