表單序列化為Json(只限input)

來源:互聯網
上載者:User

標籤:lan   sys   form   script   .net   cti   file   key   web   

轉載自:http://blog.csdn.net/itmyhome1990/article/details/42099885

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/jsp/common.jsp"%>
<!DOCTYPE html >
<html>
<head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8">
    <script type="text/javascript" src="${basePath}/system/lib/jquery-1.11.3/jquery.min.js"></script>
    <script type="text/javascript" language="JavaScript">
    (function($){
        $.fn.serializeJson = function(){
            var jsonData1 = {};//聲明一個空map
            var serializeArray = this.serializeArray();
            // 先轉換成{"id": ["12","14"], "name": ["aaa","bbb"], "pwd":["pwd1","pwd2"]}這種形式
            $(serializeArray).each(function () {
                if (jsonData1[this.name]) {//如果map有id\name\pwd鍵
                    if ($.isArray(jsonData1[this.name])) {//如果key對應value是數組
                        jsonData1[this.name].push(this.value);
                    } else {
                        jsonData1[this.name] = [jsonData1[this.name], this.value];//將key對應value設為數組
                    }
                } else {
                    jsonData1[this.name] = this.value;
                }
            });
            // 再轉成[{"id": "12", "name": "aaa", "pwd":"pwd1"},{"id": "14", "name": "bb", "pwd":"pwd2"}]的形式
            var vCount = 0;
            // 計算json內部的數組最大長度
            for(var item in jsonData1){
                var tmp = $.isArray(jsonData1[item]) ? jsonData1[item].length : 1;
                vCount = (tmp > vCount) ? tmp : vCount;
            }

            if(vCount > 1) {
                var jsonData2 = new Array();
                for(var i = 0; i < vCount; i++){
                    var jsonObj = {};
                    for(var item in jsonData1) {
                        jsonObj[item] = jsonData1[item][i];
                    }
                    jsonData2.push(jsonObj);
                }
                return JSON.stringify(jsonData2);
            }else{
                return "[" + JSON.stringify(jsonData1) + "]";
            }
        };
    })(jQuery);

    function submitUserList_4() {alert("ok");
        var jsonStr = $("#form1").serializeJson();
        console.log("jsonStr:\r\n" + jsonStr);
        /* $.ajax({
            url: "/user/submitUserList_4",
            type: "POST",
            contentType : ‘application/json;charset=utf-8‘, //佈建要求頭資訊
            dataType:"json",
            data: jsonStr,
            success: function(data){
                alert(data);
            },
            error: function(res){
                alert(res.responseText);
            }
        }); */
    }
    </script>
</head>

<body>
    <h1>submitUserList_4</h1>
    <form id="form1">
        ID:<input type="text" name="id"><br/>
        Username:<input type="text" name="name"><br/>
        Password:<input type="text" name="pwd"><br/><br/>

        ID:<input type="text" name="id"><br/>
        Username:<input type="text" name="name"><br/>
        Password:<input type="text" name="pwd"><br/><br/>
        <input type="button" value="submit" onclick="submitUserList_4()">
    </form>
</body>

表單序列化為Json(只限input)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.