您的位置:首页 > 编程语言

文件上传的动态添加输入项源代码

2011-11-26 19:33 447 查看
文件上传的动态添加输入项:
新建upload1.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>文件上传页面</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css"
href
="styles.css">
-->
<script type="text/javascript">
function addfile(){
var files=document.getElementById("files");
<!--动态添加元素-->
var input=document.createElement("input");
input.name="file";
input.type="file";

var delButton=document.createElement("input");
delButton.type="button";
delButton.value="删除";
delButton.onclick=function del(){
<!--先删除父节点-->
this.parentNode.parentNode.removeChild(this.parentNode);

}

var div=document.createElement("div");
div.appendChild(input);<!--添加文件上传的输入项-->
div.appendChild(delButton);<!--添加删除按钮-->

<!--把div块添加到files div块中-->
files.appendChild(div);

}
</script>
</head>

<body>
<form action="${pageContext.request.contextPath}/servlet/UploadServlet" method="post" enctype="multipart/form-data">
<table border="1" width="40%">
<tr>
<td>上传用户:</td><td><input type="text" name="user"></td>

</tr>

<tr>
<td></td>
<td><input type="button" value="添加文件" onClick="addfile()"/></td>
</tr>

<tr>
<td></td>
<td><div id="files"></div></td>
</tr>

<tr>
<td></td>
<td>
<input type="submit" value="上传文件"/>
</td>
</tr>
</table>
</form>
</body>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: