动态添加多个上传附件和删除
使用javascript 脚本,动态添加和删除多个文件上传
效果如下:
![1.jpg](/images/cnblogs_com/odz521/1.jpg)
![2.jpg](/images/cnblogs_com/odz521/2.jpg)
![](/Images/OutliningIndicators/ContractedBlock.gif)
AddFIles
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
<script language=javascript>
var filecount=1;
function addfile()
{
var filediv = document.getElementById("files");
var fileitemdiv = document.createElement("div");
filecount++;
var content = "<input type=file name=fileupload"+
filecount + "> <a href='javascript:removefile("+
filecount + ");'>remove</a>"
fileitemdiv.id = "fileitemdiv"+filecount;
fileitemdiv.innerHTML = content;
filediv.appendChild(fileitemdiv);
}
function removefile(fileIndex)
{
var filediv = document.getElementById("files");
var fileitemdiv = document.getElementById("fileitemdiv"+fileIndex);
filediv.removeChild(fileitemdiv);
}
</script>
</HEAD>
<BODY>
<div id=files>
<input type=file name=fileupload1 /><br/>
</div>
<input type=button value="add file" onclick="addfile();">
</BODY>
</HTML>
![1.jpg](/images/cnblogs_com/odz521/1.jpg)
![2.jpg](/images/cnblogs_com/odz521/2.jpg)
![](/Images/OutliningIndicators/ContractedBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
<script language=javascript>
var filecount=1;
function addfile()
{
var filediv = document.getElementById("files");
var fileitemdiv = document.createElement("div");
filecount++;
var content = "<input type=file name=fileupload"+
filecount + "> <a href='javascript:removefile("+
filecount + ");'>remove</a>"
fileitemdiv.id = "fileitemdiv"+filecount;
fileitemdiv.innerHTML = content;
filediv.appendChild(fileitemdiv);
}
function removefile(fileIndex)
{
var filediv = document.getElementById("files");
var fileitemdiv = document.getElementById("fileitemdiv"+fileIndex);
filediv.removeChild(fileitemdiv);
}
</script>
</HEAD>
<BODY>
<div id=files>
<input type=file name=fileupload1 /><br/>
</div>
<input type=button value="add file" onclick="addfile();">
</BODY>
</HTML>