Javascript to dynamically generate a table with a specified number of rows _ javascript tips-js tutorial

Source: Internet
Author: User
The following table uses js to generate the number of rows required by the user. The specific steps are as follows. If you are interested, refer to the following, we hope this will help you to use JavaScript to generate a table with the number of required rows.
1. First, enter the following code in the body to obtain the row value entered by the user.

The Code is as follows:







Dynamic Table Generation Line












Shows the effect:

2. Add js Code to the header

The 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 + ="














";Data + =" "+" "+" "+" "+" ";For (var I = 1; I <= Num; I ++ ){Data + =" ";Data + =" ";Data + =" ";Data + =" ";Data + =" ";}Data + ="
We areZhuzhuDudu
"+ I +"
";
Document. getElementById ("p1"). style. display = "block ";
Document. getElementById ("table1"). innerHTML = data;
}
}
Script


The generated results are as follows:

3. All code is as follows:

The 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 + "/";
%>




Test






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 + ="














";Data + =" "+" "+" "+" "+" ";For (var I = 1; I <= Num; I ++ ){Data + =" ";Data + =" ";Data + =" ";Data + =" ";Data + =" ";}Data + ="
We areZhuzhuDudu
"+ I +"
";
Document. getElementById ("p1"). style. display = "block ";
Document. getElementById ("table1"). innerHTML = data;
}
}
Script











Dynamic Table Generation Line














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.