欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

js jquery分别实现动态的文件上传操作按钮的添加和删除

程序员文章站 2022-06-20 21:15:35
代码如下:

代码如下:


<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "https://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="https://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>jquery文件上传</title>
<script type="text/javascript" src="jquery-1.7.2.js"></script>
<script type="text/javascript">
var addmore = function() {
var p = document.getelementbyid("p2");
var br = document.createelement("br");
var input = document.createelement("input");
var button = document.createelement("input");

input.setattribute("type", "file");
button.setattribute("type", "button");
button.setattribute("value", "remove");

button.onclick = function() {
p.removechild(br);
p.removechild(input);
p.removechild(button);
}

p.appendchild(br);
p.appendchild(input);
p.appendchild(button);
}
//节点的移动
//$(function(){

//});
</script>
</head>

<body>
<p id="p1">
<input type="file" id="upload"/>
<input type="button" id="btn" value="more" onclick="addmore();"/>
</p>
<p id="p2"></p>
</body>

</html>


jquery实现

. 代码如下:


<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "https://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="https://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>jquery文件上传</title>
<title>jquery1</title>
<script type="text/javascript" src="jquery-1.7.2.js"></script>
<script type="text/javascript">
/** var addmore = function() {
var p = document.getelementbyid("p2");
var br = document.createelement("br");
var input = document.createelement("input");
var button = document.createelement("input");

input.setattribute("type", "file");
button.setattribute("type", "button");
button.setattribute("value", "remove");

button.onclick = function() {
p.removechild(br);
p.removechild(input);
p.removechild(button);
}

p.appendchild(br);
p.appendchild(input);
p.appendchild(button);
}**/
//jquery实现文件上传的按钮添加和删除
$(function(){
$("input[type=button]").click(function(){
var br = $("<br>");
var input = $("<input type='file'/>");
var button = $("<input type='button' value='remove'/>");
$("#p1").append(br).append(input).append(button);

button.click(function() {
br.remove();
input.remove();
button.remove();
});
});
});
</script>
</head>

<body>
<p id="p1">
<input type="file" id="upload"/>
<input type="button" id="btn" value="more" onclick="addmore();"/>
</p>
<p id="p2"></p>
</body>
</html>