js實現(xiàn)動態(tài)增加文件域表單功能
更新時間:2018年10月22日 09:34:13 作者:dq_095
這篇文章主要為大家詳細介紹了js實現(xiàn)動態(tài)增加文件域表單功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了js實現(xiàn)動態(tài)增加文件域表單的具體代碼,供大家參考,具體內(nèi)容如下

實現(xiàn)代碼:
<html>
<head>
<title>動態(tài)添加表單元素</title>
</head>
<script language="javascript">
//全局變量,代表文件域的個數(shù),并用該變量區(qū)分文件域的name屬性
var file_count = 0;
//增加文件 域
function additem(id) {
if (file_count > 9) {
alert("最多10個 ");
return;
}
//定義行變量row;單元格變量cell;單元格內(nèi)容變量str。
var row, cell, str;
//在指定id的table中插入一行
row = eval("document.all[" + '"' + id + '"' + "]").insertRow();
if (row != null) {
//設置行的背景顏色
row.bgColor = "white";
//在行中插入單元格
cell = row.insertCell();
//設置str的值,包括一個文件域和一個刪除按鈕
str = '<input onselectstart="return false" class="tf" onpaste="return false" type="file" name="file[' + file_count + ']" style="width:500px" onkeydown="return false;"/>';
str += " <input type=" + '"' + "button" + '"' + " value=" + '"' + "刪除" + '"' + " onclick='deleteitem(this," + '"' + "tb" + '"' + ");'>";
//文件域個數(shù)增加
file_count++;
//設置單元格的innerHTML為str的內(nèi)容
cell.innerHTML = str;
}
}
//刪除文件域
function deleteitem(obj, id) {
var rowNum, curRow;
curRow = obj.parentNode.parentNode;
rowNum = eval("document.all." + id).rows.length - 1;
eval("document.all[" + '"' + id + '"' + "]").deleteRow(curRow.rowIndex);
file_count--;
}
</script>
<body>
<input type=button value="增加" onclick='additem("tb")' /><br/>
<table cellspacing="0" id="tb" style="width:400px">
</table>
</html>
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
相關文章
use jscript with List Proxy Server Information
use jscript with List Proxy Server Information...2007-06-06

