json2form

前端之家收集整理的这篇文章主要介绍了json2form前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %>

<!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 runat="server">
    <title></title>
    <script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
    <script src="Scripts/json2.js" type="text/javascript"></script>
    <script type="text/javascript">
        function test() {
            var json = $("form").form2json();
            $.post("Default3.aspx",json,function (data) {

            },"json");
        }

        $.fn.form2json = 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;
        }
    </script>
</head>
<body>
    <form id="myForm" runat="server">
    <div>
        用户名:<input type="text" id="UserName" name="UserName" /><br />
        部门:<select id="Department" name="Department">
            <option value="">请选择</option>
            <option value="1">财务部</option>
            <option value="2">技术部</option>
        </select><br />
        <input type="button" value="将所有表单元素化为JSON并提交表单" id="" onclick="test()" />
    </div>
    </form>
</body>
</html>

猜你在找的Json相关文章