我想从我在jsp页面中使用javascript创建的java类中的动态表中检索值

时间:2015-08-05 06:25:12

标签: javascript java html jsp

1.这是我用于创建动态表的java脚本代码

<!DOCTYPE html>
    <html>
    <head>
       <script>
            function myFunction(tableID) {
                var table = document.getElementById(tableID);
                var rowCount = table.rows.length;
                var row = table.insertRow(rowCount);
                var cell1 = row.insertCell(0);
                var cell2 = row.insertCell(1);
                var element2 = document.createElement("input");
                element2.type = "text";
                element2.name = "txtbox1";
                cell2.appendChild(element2);
                var cell3 = row.insertCell(2);      
                var element3 = document.createElement("select");
                element3.type = "select-one";
                element3.name = "select1";
                cell3.appendChild(element3);
                var cell4 = row.insertCell(3);
                var element4 = document.createElement("input");
                element4.type = "checkbox";
                element4.name = "chkbox1";
                cell4.appendChild(element4);
                var cell5 = row.insertCell(4);
                var element5 = document.createElement("input");
                element5.type = "text";
                element5.name = "txtbox2";
                cell5.appendChild(element5);
                var cell6 = row.insertCell(5);
                var element6 = document.createElement("input");
                element6.type = "checkbox";
                element6.name = "chkbox2";
                cell6.appendChild(element6);
                var cell7 = row.insertCell(6);
                var element7 = document.createElement("input");
                element7.type = "checkbox";
                element7.name = "chkbox3";
                cell7.appendChild(element7);
                cell1.innerHTML = rowCount;
            }

    function getValues() {
        document.write("inside getvalues")
           var values = [];
           var text = document.getElementsId('columnName');
           for (var i=0; i<text.length; ++i) {
              values.push(text[i].value);
           }
           alert(values);  //the values array will contain the text from each textarea element
        }
    </script>
    <style>
    table, td {
        border: 1px solid black;
    }
    </style>
    </head>
    <body>
    <form action="" method="post" onsubmit="getValues()">
    <h1>csv file Structure</h1>
        <table id="myTable">

            <tr>
                <td id="index">Index</td>
                <td id="columnName">Column Name</td>
                <td id="dataType">Data Type</td>
                <td id="checkbox">Checkbox</td>
                <td id="default">Default</td>
                <td id="notNull">Not Null</td>
                <td id="primaryKey">Primary Key</td>
            </tr>
        </table>

        <input type="submit" value="submit" name="createStructure">


    <br><br>
    <!-- <button onclick="getValues('myTable')">Submit</button> -->
        <br>


    </form>
    <button onclick="myFunction('myTable')">add new column</button>
    </body>
    </html>

3.提前谢谢

1 个答案:

答案 0 :(得分:0)

你可以通过使用动态参数来做到这一点。让动态参数为i,你需要改变你的元素名称 element2.name =&#34; txtbox1&#34; //旧名 改为 element2.name =&#34; txtbox&#34; + i;

我的值可以是动态生成的表中的行数。

您可以在servlet uisng request.getParameter

中获取它们