JavaScript动态数量的文件上传控件
js动态数量的文件上传控件实现代码如下所述:
<!DOCTYPEhtml>
<html>
<head>
<metahttp-equiv="Content-Type"content="text/html;charset=UTF-8">
<title>动态数量的文件上传</title>
<scripttype="text/javascript"src="jquery-2.2.0.min.js">
</script><scripttype="text/javascript">
$(function(){
vari=2;
$("#addFile").click(function(){
$(this).parent().parent().before("<trclass='file'><td>File"
+i+":</td><td><inputtype='file'name='file"
+i+"'/></td></tr>"
+"<trclass='desc'><td>Desc"
+i+":</td><td><inputtype='text'name='desc"
+i+"'/><buttonid='delete"
+i+"'>删除</button></td></tr>");
i++;
//删除
$("#delete"+(i-1)).click(function(){
var$tr=$(this).parent().parent();
$tr.prev("tr").remove();
$tr.remove();
//对i排序
$(".file").each(function(index){
varn=index+1;
$(this).find("td:first").text("File"+n);
$(this).find("td:lastinput").attr("name","file"+n);
});
$(".desc").each(function(index){
varn=index+1;
$(this).find("td:first").text("Desc"+n);
$(this).find("td:lastinput").attr("name","desc"+n);
});
});
});
});
</script>
</head>
<body>
<table>
<trclass="file">
<td>File1:</td>
<td><inputtype="file"name="file1"/></td>
</tr>
<trclass="desc">
<td>Desc1:</td>
<td><inputtype="text"name="desc1"/></td>
</tr>
<tr>
<td><inputtype="submit"id="submit"value="上传"/></td>
<td><buttonid="addFile">增加</button></td>
</tr>
</table>
</body>
</html>
好了,代码到此结束了,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的,在此也非常感谢大家对毛票票网站的支持!