|
復(fù)制代碼 代碼如下:
<script language="Javascript">
//全局變量,代表文件域的個(gè)數(shù),并用該變量區(qū)分文件域的name屬性
var file_count = 0;
//增加文件 域
function additem(id) {
if (file_count > 9) {
alert("最u22810 10個(gè)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的值,包括一個(gè)文件域和一個(gè)刪除按鈕
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"+'"'+");'>";
//文件域個(gè)數(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>
JavaScript技術(shù):JavaScript實(shí)現(xiàn)動(dòng)態(tài)增加文件域表單,轉(zhuǎn)載需保留來(lái)源!
鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。