Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
560 views
in Technique[技术] by (71.8m points)

java - Retrieving multiple textbox values with same name from JSP in Servlet

I am trying to pass values of textbox with same name from JSP to servlet but not able to accomplish this task.

I tried using this in servlet but i only get one textbox value:

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

My javascript which helps in generating multiple table rows is:

<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>

The Html table generating multiple rows:-

</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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You do not name the dynamically created input elements (to "words" or "meaning")

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...