Js dynamically generates a table with the specified number of rows

Source: Internet
Author: User

The following table uses js to generate the number of rows required by the user.
1. First, enter the following code in the body to obtain the row value entered by the user.
Copy codeThe Code is as follows:
<Table>
<Tr>
<Td> dynamically generate a table </td>
<Td> <input id = "Cold" type = "text" class = "input" size = "10" name = "Num"/> row </td>
</Tr>
</Table>
</Br>
<Input name = "" type = "button" value = "generate" class = "buttton" onclick = "table ()"/> </br>
</Br>
<Div id = "div1" style = "display: none" mce_style = "display: none">
<Div id = "table1"> </div>
</Div>
<Div id = "errmsg1" class = "formmsg"> </div>

Shows the effect:
 
2. Add js Code to the header
Copy codeThe Code is as follows:
<Script>
Function table (){
If (document. getElementById ("Num "). value = "" | document. getElementById ("Num "). value. search ("^ [0-9] * $") =-1 ){
Document. getElementById ("errmsg1"). style. display = "block"; // determines whether Num is null or not.
Document. getElementById ("errmsg1"). innerHTML = "message: the number of rows is null or not a number! ";
}
Else {
Document. getElementById ("errmsg1"). style. display = "none"; // hide the prompt information
Var Num = parseInt (document. getElementById ("Num"). value); // get the number of rows
Var flag = true;
Var data = "";
Data + = "<table> ";
Data + = "<tr>" +
"<Td> we are </td>" +
"<Td> zhuzhu </td>" +
"<Td> dudu </td>" +
"</Tr> ";
For (var I = 1; I <= Num; I ++ ){
Data + = "<tr> ";
Data + = "<td>" + I + "</td> ";
Data + = "<td> <input name = 'ldday" + I + "'Type = 'text' class = 'input'> </td> ";
Data + = "<td> <input name = 'ldcuremethod" + I + "'Type = 'text' class = 'input'> </td> ";
Data + = "</tr> ";
}
Data + = "</table> ";
Document. getElementById ("div1"). style. display = "block ";
Document. getElementById ("table1"). innerHTML = data;
}
}
</Script>

The generated results are as follows:
 
3. All code is as follows:
Copy codeThe Code is as follows:
<% @ Page language = "java" import = "java. util. *" pageEncoding = "UTF-8" %>
<%
String path = request. getContextPath ();
String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/";
%>
<! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN">
<Html>
<Head>
<Base href = "<% = basePath %>">
<Title> test </title>
<Meta http-equiv = "pragma" content = "no-cache">
<Meta http-equiv = "cache-control" content = "no-cache">
<Meta http-equiv = "expires" content = "0">
<Meta http-equiv = "keywords" content = "keyword1, keyword2, keyword3">
<Meta http-equiv = "description" content = "This is my page">
<Style type = "text/css">
. Right {
Margin: 0% 10%;
Width: 600px;
}
. Right table {
Background: white;
Width: 100%;
Border: 1px solid #499B33;
}
. Right td {
Background: blue;
Text-align: center;
Padding: 2px;
Border: 1px solid #499B33;
}
. Right td {
Background: # 8FBC8F;
}
. Item {
Text-align: center;
Width: 100px;
}
. Assigned {
Border: 1px solid # BC2A4D;
}
</Style>
<Script>
Function table (){
If (document. getElementById ("Num "). value = "" | document. getElementById ("Num "). value. search ("^ [0-9] * $") =-1 ){
Document. getElementById ("errmsg1"). style. display = "block"; // determines whether payNum is null or not.
Document. getElementById ("errmsg1"). innerHTML = "message: the number of rows is null or not a number! ";
}
Else {
Document. getElementById ("errmsg1"). style. display = "none"; // hide the prompt information
Var Num = parseInt (document. getElementById ("Num"). value); // gets the number of minutes
Var flag = true;
Var data = "";
Data + = "<table> ";
Data + = "<tr>" +
"<Td> we are </td>" +
"<Td> zhuzhu </td>" +
"<Td> dudu </td>" +
"</Tr> ";
For (var I = 1; I <= Num; I ++ ){
Data + = "<tr> ";
Data + = "<td>" + I + "</td> ";
Data + = "<td> <input name = 'ldday" + I + "'Type = 'text' class = 'input'> </td> ";
Data + = "<td> <input name = 'ldcuremethod" + I + "'Type = 'text' class = 'input'> </td> ";
Data + = "</tr> ";
}
Data + = "</table> ";
Document. getElementById ("div1"). style. display = "block ";
Document. getElementById ("table1"). innerHTML = data;
}
}
</Script>
</Head>
<Body>
<Br>
<Div style = "width: 750px;">
<Div class = "right">
<Table>
<Tr>
<Td> dynamically generate a table </td>
<Td> <input id = "Num" type = "text" class = "input" size = "10" name = "Num"/> row </td>
</Tr>
</Table>
</Br>
<Input name = "" type = "button" value = "generate" class = "buttton" onclick = "table ()"/> </br>
</Br>
<Div id = "div1" style = "display: none" mce_style = "display: none">
<Div id = "table1"> </div>
</Div>
<Div id = "errmsg1" class = "formmsg"> </div>
</Div>
</Div>
</Body>
</Html>

Related Article

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.