JavaScript實(shí)現(xiàn)動態(tài)增加文件域表單
更新時間:2009年02月12日 20:30:05 作者:
對于上傳多個文件,可以通過js來動態(tài)生成文件域,下面是源代碼,收藏在這里,供以后直接使用,hoho!
js代碼:
<script language="javascript">
//全局變量,代表文件域的個數(shù),并用該變量區(qū)分文件域的name屬性
var file_count = 0;
//增加文件 域
function additem(id) {
if (file_count > 9) {
alert("最u22810 10個u25991 件u22495 ");
return;
}
//定義行變量row;單元格變量cell;單元格內(nèi)容變量str。
var row,cell,str;
//在指定id的table中插入一行
row = eval("document.all["+'"'+id+'"'+"]").insertRow();
if(row != null ) {
//設(shè)置行的背景顏色
row.bgColor="white";
//在行中插入單元格
cell = row.insertCell();
//設(shè)置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++;
//設(shè)置單元格的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>
html代碼:
<input type=button value="增加" onclick='additem("tb")'/><br/>
<table cellspacing="0" id="tb" style="width:400px">
</table>
復(fù)制代碼 代碼如下:
<script language="javascript">
//全局變量,代表文件域的個數(shù),并用該變量區(qū)分文件域的name屬性
var file_count = 0;
//增加文件 域
function additem(id) {
if (file_count > 9) {
alert("最u22810 10個u25991 件u22495 ");
return;
}
//定義行變量row;單元格變量cell;單元格內(nèi)容變量str。
var row,cell,str;
//在指定id的table中插入一行
row = eval("document.all["+'"'+id+'"'+"]").insertRow();
if(row != null ) {
//設(shè)置行的背景顏色
row.bgColor="white";
//在行中插入單元格
cell = row.insertCell();
//設(shè)置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++;
//設(shè)置單元格的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>
html代碼:
復(fù)制代碼 代碼如下:
<input type=button value="增加" onclick='additem("tb")'/><br/>
<table cellspacing="0" id="tb" style="width:400px">
</table>
您可能感興趣的文章:
相關(guān)文章
select 控制網(wǎng)頁內(nèi)容隱藏于顯示的實(shí)現(xiàn)代碼
js下通過select的控制實(shí)現(xiàn)內(nèi)容的隱藏于顯示,需要的朋友可以參考下。2010-05-05用javascript實(shí)現(xiàn)文本框和"選擇"按扭之間的間距
用javascript實(shí)現(xiàn)文本框和"選擇"按扭之間的間距...2007-07-07js正則實(shí)現(xiàn)的密碼框簡單制作,還可以替換成自己想用得符號
js正則實(shí)現(xiàn)的密碼框簡單制作,還可以替換成自己想用得符號...2007-10-10