Use js to dynamically add text boxes to the foreground, and obtain text box content in the background (sample code)

Source: Internet
Author: User

There are two pages:

1. create. jsp
Copy codeThe Code is as follows:
<% @ Page language = "java" import = "java. util. *" pageEncoding = "UTF-8" %>

<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<Html>
<Title>
</Title>
<Head>
</Head>
<Body>
<Form id = "form1" action = "request. jsp">
<Script language = "javascript" type = "text/javascript">

Var count = 0;
Function additem (id)
{
Var row, cell, str;
Row = document. getElementByIdx_x (id). insertRow ();
If (row! = Null)
{
Cell = row. insertCell ();
Cell. innerHTML = "<input id = \" St "+ count +" \ "type = \" text \ "name = \" St "+ count +" \ "value = \" st "+ count +" \ "> <input type = \" button \ "value = \" delete \ "onclick = \ 'deleteitem (this ); \ '> ";
Count ++;
}
}
Function deleteitem (obj)
{
Var curRow = obj. parentNode. parentNode;
Tb. deleteRow (curRow. rowIndex );
}

Function getsub ()
{
Var re = "";
For (var I = 0; I <count; I ++)
{
Re + = document. getElementsByName ("St" + I) [0]. value;

}
Document. getElementByIdx_x ("Hidden1"). value = re;
}


</Script> <table id = "tb"> </table>
<Input name = "button" type = "button" onclick = 'additem ("tb ") 'value = "add>"/> <input type = "submit" name = "submit" onclick = "getsub ()" value = "submit"/>
<Input id = "Hidden1" name = "Hidden1" type = "hidden" value = ""/>
</Form>
</Body>

</Html>

Ii. Receiving data page request. jsp
Copy codeThe Code is as follows:
<% @ Page language = "java" import = "java. util. *" pageEncoding = "UTF-8" %>

<Html>
<Head>

<Title> test </title>

</Head>
<%
String data = request. getParameter ("Hidden1 ");
Out. println ("the obtained data is:" + data );
%>
<Body>

</Body>
</Html>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.