类似这个: 假如开始本来就有2个输入框,然后点按钮加一次就增加2个输入框,依此类推,最后重点是要写保存语句,谢谢啦 <script language="JavaScript">
function addFile()
{
var str = ' <INPUT type="text" size="50" NAME="name1"> <INPUT type="text" size="50" NAME="name2"> <br>'
document.getElementById('MyFile').insertAdjacentHTML("beforeEnd",str)
}
</script>
<INPUT type="text" size="50" NAME="name1"> <INPUT type="text" size="50" NAME="name2">
<input type="button" value="增加(Add)" onclick="addFile()">
<asp:Button Runat="server" Text="保存" ID="Upload" OnClick="Upload_Click"> </asp:Button>
function addFile()
{
var str = ' <INPUT type="text" size="50" NAME="name1"> <INPUT type="text" size="50" NAME="name2"> <br>'
document.getElementById('MyFile').insertAdjacentHTML("beforeEnd",str)
}
</script>
<INPUT type="text" size="50" NAME="name1"> <INPUT type="text" size="50" NAME="name2">
<input type="button" value="增加(Add)" onclick="addFile()">
<asp:Button Runat="server" Text="保存" ID="Upload" OnClick="Upload_Click"> </asp:Button>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Example</title>
<script type="text/javascript">
function a() {
alert(1);
var tab = document.getElementById("tab");
tab.appendChild(document.createElement("tr"));
var td = document.createElement("td");
tab.appendChild(td);
var i = document.createElement("input");
td.appendChild(i);
}
</script> </head>
<body>
<table id='tab'>
<tr>
<td><input type="text"></input></td>
<tr>
<td><input type="text"></input></td> <input type="button" onclick="a()"></input>
</table>
</body></html>
主要就是
createElement
appendChild
这两个api了~。
<head>
<title>Example</title>
<script type="text/javascript">
function a() {
alert(1);
var tab = document.getElementById("tab");
tab.appendChild(document.createElement("tr"));
var td = document.createElement("td");
tab.appendChild(td);
var i = document.createElement("input");
td.appendChild(i);
}
</script> </head>
<body>
<table id='tab'>
<tr>
<td><input type="text"></input></td>
<tr>
<td><input type="text"></input></td> <input type="button" onclick="a()"></input>
</table>
</body></html>
{
var str="<br><input type='text' size='20' name='txtBox'>"
document.getElementById("MyText").insertAdjacentHTML("beforeend",str);
}
</script>
<p id="MyText">
<input name="txtBox" type="text"><br>
<input type="button" id="add" value="增加">
</p>
for(m=1;m <4;m++){
alert(document.getElementsByName("text")[0].value);
}
string[] str= System.Web.HttpContext.Current.Request.Form.GetValues("txtBox");
int j=1;
TextBox txt = new TextBox();
txt.ID = "txt_" + j.ToString();
this.Page.Form.Controls.Add(txt);objText = ((TextBox)form1.FindControl("txt_" + j.ToString())).Text.ToString();
<head>
<title>Example </title>
<script type="text/javascript">
function a() {
alert(1);
var tab = document.getElementById("tab");
tab.appendChild(document.createElement("tr"));
var td = document.createElement("td");
tab.appendChild(td);
var i = document.createElement("input");
td.appendChild(i);
}
</script> </head>
<body>
<table id='tab'>
<tr>
<td> <input type="text"> </input> </td>
<tr>
<td> <input type="text"> </input> </td> <input type="button" onclick="a()"> </input>
</table>
</body> </html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<META http-equiv="Content-Style-Type" content="text/css">
<script language="javascript">
function addInput(){
var div=document.getElementById("inputDiv");
var input=document.createElement("input");
div.appendChild(input);
}
function decInput(){
var div=document.getElementById("inputDiv");
var nodeLen=div.childNodes.length;
if(div.childNodes[nodeLen-1]!=null&&nodeLen>4)
div.removeChild(div.childNodes[nodeLen-1]);
}
window.onload=function(){
}
</script>
</head>
<body>
<div id="inputDiv"><input type="text" /><input type="button" onclick="addInput()" value="+" ></inpit><input type="button" onclick="decInput()" value="-" ></inpit></div>
<div></div>
</body>
</html>
录入界面a.html
<html>
<head>
<script language="javascript">
var i = 1 ;
function addInput(){
var div = document.getElementById("inputDiv") ;
var str = "<input type='text' id='id1"+i+"' name='name1"+i+"'>" ;
str += " " ;
str += "<input type='text' id='id2"+i+"' name='name2"+i+"'>" ;
div.innerHTML += str ;
i++ ;
document.getElementById("num").value = i + "" ;//没添加一次,将总数累加
}
</script>
</head>
<body>
<div id="inputDiv">
<form name="form1" action="b.jsp">
<input type="text" id="id10" name="name10"/>
<input type="text" id="id20" name="name20"/>
<!-- 注意起的名称,name10 name20 后面的“0”是累加用的,前面部分是区分这两个文本域 -->
</form>
</div>
<br>
<input type="button" onclick="addInput()" value="+" >
<input type="submit" value="submit" >
<input type="hidden" id="num" name="num" value="1"><!-- 记录添加次数 -->
</body>
</html>
保存处理页面
b.jsp(不好意思我用jsp页面惯了)
<%
//先获取添加记录的次数
String num = request.getParameter("num") ;
//将读取的次数由 字符串 转为 数值型
int n = Integer.parseInt(num) ;
//循环取文本域的值
for(int i=0;i<n;i++){
String name1 = request.getParameter("name1"+i) ;
String name2 = request.getParameter("name2"+i) ;
}
%>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<script language="javascript">
function add()
{
option = new Array();
proportion = new Array();
str='<table>';
str=str+'<tr align=center valign=middle bgcolor=#FFFFFF>';
str=str+'<td width="100%" height="25"><input type="text"></td>';
str=str+'<td width="100%" height="25"><input type="button" name=del onclick="this.parentNode.parentNode.parentNode.deleteRow(this.parentNode.parentNode.rowIndex)" value="删除"></td>';
str=str+'</tr></table>';
window.upid.innerHTML+=str+'';
}
</script>
</head>
<body>
<form name="myform" onsubmit="javascript:return check()">
<table>
<tr>
<td>
<input name="Submit2" type="button" onclick="javascript:add()"
value="增加">
</td>
</tr>
<tr>
<td>
<div id="upid"></div>
</td>
</tr>
</table>
</form>
</body>
</html>
{
var str=" <br> <input type='text' size='20' name='txtBox'>"
document.getElementById("MyText").insertAdjacentHTML("beforeend",str);
}
</script>
<p id="MyText">
<input name="txtBox" type="text"> <br>
<input type="button" id="add" value="增加">
</p>
for(m=1;m <4;m++){
alert(document.getElementsByName("text")[0].value);
}
string[] str= System.Web.HttpContext.Current.Request.Form.GetValues("txtBox");
int j=1;
TextBox txt = new TextBox();
txt.ID = "txt_" + j.ToString();
this.Page.Form.Controls.Add(txt); objText = ((TextBox)form1.FindControl("txt_" + j.ToString())).Text.ToString();