简体   繁体   English

在jsp中添加和检索文本框

[英]Add and retrieve text boxes in jsp

I want to add textbox dynamically in jsp page on "Add Row" button click. 我想在“添加行”按钮上单击的jsp页面中动态添加文本框。 I have written java script to add it. 我已经编写了Java脚本来添加它。 No issues with that. 没问题。 But I am not able to retrieve those values in Servlet page. 但是我无法在Servlet页面中检索这些值。 Any ideas? 有任何想法吗?

Here is the script: 这是脚本:

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 cell3 = row.insertCell(1);
        var element2 = document.createElement("input");
        element2.type = "text";
        cell3.appendChild(element2);

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

         var cell4 = row.insertCell(3);
        var element4 = document.createElement("input");
        element4.type = "text";
        cell4.appendChild(element4);

    }

And here is the jsp where script is called: 这是称为脚本的jsp:

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

You need to specify the name attribute. 您需要指定name属性。 It becomes the request parameter name. 它成为请求参数名称。

You can just give them all the same name. 您可以给他们全部相同的名字。

element.name = "foo";

Or if you want to "respect" IE6/7 users as well 或者,如果您也想“尊重” IE6 / 7用户

document.createElement('<input type="text" name="foo">');

( jQuery makes it all easier and better crossbrowser compatible) jQuery使这一切变得更容易和更好地兼容跨浏览器)

Then you can access them all as follows in the servlet 然后,您可以按照以下步骤在servlet中访问它们。

String[] foos = request.getParameterValues("foo");

They will appear in the order as they appear in the HTML DOM tree. 它们将按照出现在HTML DOM树中的顺序出现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM