編輯表格輸入內容、根據input輸入框輸入數字動態生成表格行數、編輯表格內容提交傳給後臺數據處理

編輯表格輸入內容、根據input輸入框輸入數字動態生成表格行數、編輯表格內容提交傳給後臺數據處理javascript

記錄本身學習作的東西,寫的小demo,但願對你們也有幫助!css

代碼以下:html

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <link rel="stylesheet" href="../../js/bootstrap-3.3.7-dist/css/bootstrap.min.css" />
    <script type="text/javascript" src="../../js/jquery-1.12.1.min.js"></script>
    <script type="text/javascript" src="../../js/bootstrap-3.3.7-dist/js/bootstrap.min.js"></script>
    <style type="text/css">
        table thead tr th {
            border-bottom: 0 !important;
        }
        
        .table {
            margin-top: 20px;
            width: 80%;
            margin-left: 20px;
        }
        
        table tr,
        th,
        td {
            text-align: center;
        }
        
        .tdpadding {
            padding: 0 !important;
        }
        
        .table_input {
            width: 100%;
            height: 37px;
            border: none;
        }
    </style>

    <body>

        <p id="demo"></p>
        <label>樓號:</label><input name="" type="text" class="louhao">
        <label>單元數:</label><input type="text" id="myInput" oninput="myFunction()">
        <form id="submitForm">
            <table class="table table-bordered">
                <thead class="aa">
                    <tr>
                        <th>單元</th>
                        <th>開始樓層</th>
                        <th>結束樓層</th>
                        <th>每層次數</th>
                    </tr>
                </thead>
                <tbody class="units">
                </tbody>
            </table>
        </form>
    </body>
    <button class="btn">提交</button>
    <script>
        function myFunction() {
            var x = $("#myInput").val();
            $("#demo").text("你輸入的是: " + x);
            $(".units").html("");
            var str = ""
            for(var i = 0; i < x; i++) {
                str += "<tr><td class='tdpadding'><input name='inp0' value='" + (i + 1) + "' type='text' readonly='readonly' class='table_input desa'></td><td class='tdpadding'><input name='inp1' value='' type='text' class='table_input'></td><td class='tdpadding'><input name='inp2' value='' type='text' class='table_input'></td><td class='tdpadding'><input name='inp3' value='' type='text' type='text' class='table_input'></td></tr>"
            }
            $(".units").append(str)
        }
        $(".btn").click(function() {
            var louhao = $(".louhao").val()
            console.log(louhao)
            var msg = $("#submitForm").serialize();
            var json = "[{";
            var msg2 = msg.split("&"); //先以「&」符號進行分割,獲得一個key=value形式的數組
            var t = false;
            for(var i = 0; i < msg2.length; i++) {
                var msg3 = msg2[i].split("="); //再以「=」進行分割,獲得key,value形式的數組
                for(var j = 0; j < msg3.length; j++) {
                    json += "\"" + msg3[j] + "\"";
                    if(j + 1 != msg3.length) {
                        json += ":";
                    }
                    if(t) {
                        json += "}";
                        if(i + 1 != msg2.length) { //表示是否到了當前行的最後一列
                            json += ",{";
                        }
                        t = false;
                    }
                    if(msg3[j] == "inp3") { //這裏的「inp3」是你的表格的最後一列的input標籤的name值,表示是否到了當前行的最後一個input
                        t = true;
                    }
                }
                if(!msg2[i].match("inp3")) { //同上
                    json += ";";
                }

            }
            json += "]";
            console.log(json)
            //最終msg的值就被轉換爲:[{"key":"value";"key":"value"},{"key":"value";"key":"value"}]格式的json數據<br>
        })
    </script>
</html>
相關文章
相關標籤/搜索