Javascript| News | questions
</pre>
Javascript dynamic multiple-line input problem:
input.jsp
<%@ page contenttype= "Text/html;charset=big5"%>
<%request.setcharacterencoding ("Big5");%>
<script language= "JavaScript" >
function Tbladdrow ()
{
var i=lines.rows.length;
var row = Table1.insertrow (Table1.rows.length);
var col = Row.insertcell (0);
col.innerhtml = "<input id=line[" +i+ "][name1" name=line["+i+]name1";
Col = Row.insertcell (1);
col.innerhtml = "<input id=line[" +i+ "][name2" name=line["+i+]name2";
Col = Row.insertcell (2);
col.innerhtml = "<input id=line[" +i+ "][name3" name=line["+i+]name3";
Col = Row.insertcell (3);
col.innerhtml = "<input id=line[" +i+ "][name4" name=line["+i+]name4";
}
function Tbladdrows (items)
{
for (i = 1; I <= items; i++)
{
Tbladdrow ();
}
}
function Delrow ()
{
if (lines.rows.length==0)
{
return false;
}
Lines.deleterow ();
}
</script>
<meta http-equiv= "Content-type" content= "text/html; Charset=big5 ">
<title> input </title>
<body>
<form action= "getinput.jsp" >
<div align=center><font size=2> Input tests </FONT></DIV></TD>
<TD width=412 bgcolor= #e4e4e4 >
<table id=table1 cellspacing=0 bordercolordark= #ffffff cellpadding=0
width=350 align=center bordercolorlight= #000000 border=1>
<TBODY>
<tr bgcolor= #999999 >
<th Width=94><font size=2> a </FONT></TH>
<th width=84><font size=2> Parameters two </FONT></TH>
<th Width=85><font size=2> Reference three </FONT></TH>
<th width=77><font size=2> Parameters four </FONT></TH></TR>
<tbody id=lines name= "lines" ><!--
<TR>
<td><input id= "Things_name" type= "text" name= "Things_name" size= "></TD>"
<td><input id= "Things_model" type= "text" name= "Things_model" size= "ten" ></TD>
<td><input id= "Things_number" type= "text" name= "Things_number" size= "5" ></TD>
<td><input id= "Things_unit" type= "text" name= "Things_unit" size= "5" ></TD>
</TR>
--></tbody></table>
<div align=center><br></div>
<div align=center>
<input id=items Type=hidden value=1 name=items> <input (Onclick=tbladdrow); Type=button value= New Line name=insert/>
<font color= "#e4e4e4" > </font>
<input language=javascript Type=button value= Delete Row name=del/>
</DIV></TD></TR></TBODY></TABLE>
<input type= "Submit" value= "Commit"/>
</form>
</body>
getinput.jsp
<%@ page contenttype= "Text/html;charset=big5"%>
<%@ page import= "java.util.*"%>
<%
Request.setcharacterencoding ("Big5");
for () {
/*int Items =integer.parseint (request.getparameter ("items"));
OUT.PRINTLN (items);
for (int i=1; i<=items; i++) {* * *
Get the user ' s input text name
Enumeration Paramnames=request.getparameternames ();
String Paramname=null;
while (Paramnames.hasmoreelements ()) {
Paramname= (String) paramnames.nextelement ();
String Paramvalues=request.getparameter (paramname);
Out.println ("<font color=red>param ' name </font>" +paramname+ ");
Out.println ("<font color=red>paramvalues</font>" +paramvalues+ "<br>");
Out.println ("***<br>");
/*string Param=request.getparameter (paramname);
Out.println (paramname+ "" +param+ "<br>");
/*string paramvalues[]=request.getparametervalues (paramname);
for (int i=0; i<=paramvalues.length-1; i++)
{
Out.println ("<font color=red>param ' name </font>" +paramname+ ");
Out.println ("<font color=red>pramvalues[" +i+ "]</font>" +paramvalues[i]+ "<br>");
*/
}
Out.println ("<br>" +name);
%>
<meta http-equiv= "Content-type" content= "text/html; Charset=big5 ">
<title> Accept Input </title>
<body>
</body>
Operation Effect:
Param ' name line[0]name2 paramValues12
Param ' name Line[0]name3 paramValues13
Param ' Name Items paramValues1
Param ' name line[0]name1 paramValues11
Param ' name Line[1]name4 paramValues24
Param ' name line[1]name1 paramValues21
Param ' name Line[0]name4 paramValues14
Param ' name Line[1]name3 paramValues23
Param ' name line[1]name2 paramValues22
We can see that we can get the correct value, but, there is a problem, the value is chaotic, not regular appearance, we want to insert the value of the database, we need to organize and then insert (nonsense). What do we do?
If we know how to enter a few lines, this problem can be almost solved, but how do we know that the previous page is dynamic input by JavaScript, I finally found the solution in confusion:
Param ' Name Items paramValues1
This 1 is not what we want, but it represents the number of columns!
It's a good thing to do!
After a few changes to the code, I finally realized my own idea
<%@ page contenttype= "Text/html;charset=big5"%>
<%@ page import= "java.util.*"%>
<%
Request.setcharacterencoding ("Big5");
Receive Parammeter ' s name as the type of enumeration
/*enumeration Paramnames=request.getparameternames ();
String Paramname=null;
Paramname= (String) paramnames.nextelement ();
String Paramvalues=request.getparameter (paramname);
int column=0;
String tempcol=request.getparameter ("items"). Trim ();
Column=integer.parseint (Tempcol);//Notice exception!
while (Paramnames.hasmoreelements ()) {
for (int i=0;i<=column; i++)
{
for (int j=1;j<=4; j + +)
{
String getinputtemp= "line[" +i+ "]name" +J;
Out.println (getinputtemp);
Out.println ("<font color=red>line[" +i+ "]" + "name[" +j+ "]</font>" +request.getparameter (getInputTemp));
}
Out.println ("<br>");
}
%>
<meta http-equiv= "Content-type" content= "text/html; Charset=big5 ">
<title> Accept Input </title>
<body>
</body>
Here we do not use enumeration, Paramname= (String) paramnames.nextelement (); Why, because I already know the name of the input parameter! Actually think a very simple, hehe
To sum up, the dynamic input of JavaScript code is Google search, and it is the code is PHP, and I want to use JSP to accept the input of the code. The code about JavaScript can be read, but I can't write it, but It seems to be enough now. The original blog wrote an article on this issue, the study again (forced, no way), finally resolved.
In fact, think of data structure, here is a fixed columns of the two-dimensional array, if you think of this, solve the problem will soon, here, think of the data structure of their own grasp is not enough, and seems to have no use, if as a programmer but can not master the data Stutcture, only repent and bow your head to learn!
Originally thought it could, however, I tested more than 3columns of data, suddenly found that the error! What about the items? Traversal gets the total number of parameter names -1/4
<%@ page contenttype= "Text/html;charset=big5"%>
<%@ page import= "java.util.*"%>
<%
Request.setcharacterencoding ("Big5");
Receive Parammeter ' s name as the type of enumeration
/*enumeration Paramnames=request.getparameternames ();
String Paramname=null;
Paramname= (String) paramnames.nextelement ();
String Paramvalues=request.getparameter (paramname);
int columntemp=0;
String Paramname=null;
Enumeration Paramnames=request.getparameternames ();
while (Paramnames.hasmoreelements ()) {
Paramname= (String) paramnames.nextelement ();
columntemp++;
}
int column=0;
column= (columnTemp-1)/4;
Out.println (column+ "<br>");
for (int i=0;i<column; i++)
{
for (int j=1;j<=4; j + +)
{
String getinputtemp= "line[" +i+ "]name" +J;
Out.println (getinputtemp);
Out.println ("<font color=red>line[" +i+ "]" + "name[" +j+ "]</font>" +request.getparameter (getInputTemp));
}
Out.println ("<br>");
}
%>
<meta http-equiv= "Content-type" content= "text/html; Charset=big5 ">
<title> Accept Input </title>
<body>
</body>
It's finally okay here, but
String Paramname=null;
Enumeration Paramnames=request.getparameternames ();
while (Paramnames.hasmoreelements ()) {
Paramname= (String) paramnames.nextelement ();//Necessary
columntemp++;
}
Required Paramname= (String) paramnames.nextelement ();//Necessary
Otherwise can not traverse, or die like, do not know why?
Like Resutltset Rs.next () can traverse (pagination), High man please enlighten, moonsoft_su@126.com!
</pre>