<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script type="text/javascript">
var count = 0;
function Add(num){ count+= 1; var File1 = document.getElementById("file1");
var div = document.createElement("div");
div.style.position = "relative";
div.style.zIndex = "9999";
div.style.width = "560px";
div.style.height = "10px";
//newDiv.style.left = (parseInt(document.body.scrollWidth) - 300) / 2 + "px"; // 屏幕居中
div.style.background = "#EFEFEF";
div.style.border = "1px solid #860001";
div.style.padding = "10px";
div.style.margin = "25px";
var countTxt = document.createTextNode("章节"+count);
var inputTxt = document.createElement("input");
inputTxt.type = "text";
inputTxt.value = document.getElementById(num).value;
inputTxt.name = "txt"+count;
var inputTxta = document.createElement("input");
inputTxta.type = "text";
inputTxta.value = document.getElementById("a"+num).value;
inputTxta.name = "txt"+count;
var inputTxtaa = document.createElement("input");
inputTxtaa.type = "text";
inputTxtaa.value = document.getElementById("aa"+num).value;
inputTxtaa.name = "txt"+count; var btn = document.createElement("input");
btn.type = "button";
btn.value = "删除";
btn.onclick=function()
{
this.parentNode.parentNode.removeChild(this.parentNode); var n = File1.getElementsByTagName("div");
for(var k=0; k<n.length; k++)
{
n[k].firstChild.nodeValue = "章节"+(k+1);
}
count -= 1;
} div.appendChild(countTxt);
div.appendChild(inputTxt);
div.appendChild(inputTxta);
div.appendChild(inputTxtaa); div.appendChild(btn);
File1.appendChild(div);
}
</script>
<body><div id="file1"> </div>
<form name="addChapter" action="">
<input name="CourseID" id="1" value="CourseID" type="hidden" />
<input name="ChapterID" type="hidden" id="a1" value="ChapterID"/>
<input name="ChapterName" type="hidden" id="aa1" value=" ChapterName" />
<input name="xiyaonamebutton" value="增加章节" type="button" onclick="Add(1);" style="margin-left:23px;" />
<input name="submit" value="submit" type="submit" style="width:75px;"/>
</form>
</body>
</html>
最后将此表单提交,进入action....
2.增加一个hidden类型的参数来控制你增加了多少行,每增加一行该变量+1.例如第一行的文本框就应该是
<input type="text" name="user1" id="user1" value="">
<input type="hidden" name="arg" id="arg" value="1">不过需要注意一点就是,假设在删除时你删除了中间的一行,那么所有的name和id都需要重新赋值~
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script type="text/javascript">
var count = 0;
function Add(num){ count+= 1; var File1 = document.getElementById("file1");
var div = document.createElement("div");
div.style.position = "relative";
div.style.zIndex = "9999";
div.style.width = "560px";
div.style.height = "10px";
//newDiv.style.left = (parseInt(document.body.scrollWidth) - 300) / 2 + "px"; // 屏幕居中
div.style.background = "#EFEFEF";
div.style.border = "1px solid #860001";
div.style.padding = "10px";
div.style.margin = "25px";
var countTxt = document.createTextNode("章节"+count);
var inputTxt = document.createElement("input");
inputTxt.type = "text";
inputTxt.value = document.getElementById(num).value;
inputTxt.name = "txt"+count;
inputTxt.id = "CourseID"+count;
var inputTxta = document.createElement("input");
inputTxta.type = "text";
inputTxta.value = document.getElementById("a"+num).value;
inputTxta.name = "txt"+count;
inputTxta.id = "ChapterID"+count;
var inputTxtaa = document.createElement("input");
inputTxtaa.type = "text";
inputTxtaa.value = document.getElementById("aa"+num).value;
inputTxtaa.name = "txt"+count;
inputTxtaa.id = "ChapterName"+count;
var btn = document.createElement("input");
btn.type = "button";
btn.value = "删除";
btn.onclick=function(){
this.parentNode.parentNode.removeChild(this.parentNode); var n = File1.getElementsByTagName("div");
for(var k=0; k<n.length; k++){
n[k].firstChild.nodeValue = "章节"+(k+1);
}
count -= 1;
} div.appendChild(countTxt);
div.appendChild(inputTxt);
div.appendChild(inputTxta);
div.appendChild(inputTxtaa); div.appendChild(btn);
File1.appendChild(div);
} function getValue(){
if(count){
alert("请增加章节");
return false;
}
document.getElementById("hidCount").value = count;
return true;
}
</script>
</head>
<body>
<div id="file1"></div>
<form name="addChapter" action="" onSubmit="return getValue();">
<input type="hidden" id="hidCount" value="0" />
<input name="CourseID" id="1" value="CourseID" type="hidden" />
<input name="ChapterID" type="hidden" id="a1" value="ChapterID" />
<input name="ChapterName" type="hidden" id="aa1" value=" ChapterName" />
<input name="xiyaonamebutton" value="增加章节" type="button" onclick="Add(1);" style="margin-left: 23px;" />
<input name="submit" value="submit" type="submit"style="width: 75px;" />
</form>
</body>
</html>
每次增加章节的时候,要给增加的标签赋值id的值,这里一定要根据count来定,否定会重复。
form表单中添加了一个隐藏表单域。
提交action后,在Action里获取隐藏表单域hidCount的值,根据它的值来循环取出页面上标签的值。int count = request.getParamter("hidCount");
List info = new ArrayList();
for(int i=0; i<count; i++){
info.add(request.getParamter("CourseID"+i);
info.add(request.getParamter("ChapterID"+i);
info.add(request.getParamter("ChapterName"+i);
}java代码我是直接在csdn这个回复框里敲的,比如有什么类型转换之类的错误,lz自己再改改。
count值代表前台用户添加了几行章节,然后循环分别取出每个文本框的值就行。