从Servlet中的JSP检索具有相同名称的多个文本框值

时间:2012-05-02 20:41:36

标签: java javascript jsp servlets

我正在尝试将具有相同名称的文本框的值从JSP传递到servlet,但无法完成此任务。

我尝试在servlet中使用它,但我只获得一个文本框值:

String[] words = request.getParameterValues("words");
String[] meanings = request.getParameterValues("meaning");

我的javascript有助于生成多个表行:

<SCRIPT language="javascript">
        function addRow(tableID) {

            var table = document.getElementById(tableID);

            var rowCount = table.rows.length;
            var row = table.insertRow(rowCount);

            var cell1 = row.insertCell(0);
            var element1 = document.createElement("input");
            element1.type = "checkbox";
            cell1.appendChild(element1);

            var cell2 = row.insertCell(1);
            var element2 = document.createElement("input");
            element2.type = "text";

            cell2.appendChild(element2);

            var cell3 = row.insertCell(2);
            var element3 = document.createElement("input");
            element3.type = "text";
            cell3.appendChild(element3);

        }

        function deleteRow(tableID) {
            try {
            var table = document.getElementById(tableID);
            var rowCount = table.rows.length;

            for(var i=0; i<rowCount; i++) {
                var row = table.rows[i];
                var chkbox = row.cells[0].childNodes[0];
                if(null != chkbox && true == chkbox.checked) {
                    table.deleteRow(i);
                    rowCount--;
                    i--;
                }

            }
            }catch(e) {
                alert(e);
            }
        }

    </SCRIPT>

Html表生成多行: -

</body>
<form name="managelist" action="../createlistpath" method="post">

    <TABLE id="dataTable" width="600px" border="0">
    <tr>
    <td><b> List Name</b></td>
    <td colspan="2"><input type="text" name="listname"></td>
    </tr>

    <tr >
        <td><b>Select</b></td>
        <td><b>Word</b></td>
        <td><b>Meaning</b></td>
    </tr>
        <TR >
            <TD><INPUT type="checkbox" name="checkbox"/></TD>
            <TD> <INPUT type="text" name="words" /> </TD>
            <TD> <INPUT type="text" name="meaning" /> </TD>
        </TR>
    </TABLE>

 <INPUT type="button" value="Add Row" onclick="addRow('dataTable')" />

    <INPUT type="button" value="Delete Row" onclick="deleteRow('dataTable')" />
<br>
<input type="submit" value = "submit" name="submit">

</form>
</body>

2 个答案:

答案 0 :(得分:5)

您没有为动态创建的输入元素命名(至&#34;单词&#34;或&#34;含义&#34;)

        var element2 = document.createElement("input");
        element2.name = "words";
        element2.type = "text";
        cell2.appendChild(element2);

答案 1 :(得分:5)

如果你有这样的表格:

<form method="post">
    <input type="text" name="words"><br>
    <input type="text" name="words"><br>
    <input type="submit">
</form>

如果您在文本框中放置一个两个,则发送

words=one&words=two

获取,

String[] words = request.getParameterValues("words");