本文非原創(chuàng)蒂胞,在網上找到了可行的方法,做記錄条篷。
通過JS實現骗随,點擊button給table增加行,點擊button刪除一行拥娄,效果如下:
修改時蚊锹,注意修改頁面組件id。
html
<table align="center" id="drlFileTable" border="1">
<tr align="center">
<td width="256">文件</td>
<td width="256">描述</td>
<td><input type="button" name="add" value="添加" onclick="addRow()"/>
<input name='drlTRLastIndex' type='hidden' id='drlTRLastIndex' value="2"/>
</td>
</tr>
<tr align="center" id="drlItem1">
<td>
<input name='file' type='file' />
</td>
<td>
<input name='description' style="width:256px;" type='text'/>
</td>
<td>
<input id='txtDel1' type='button' value='刪除' onclick="delRow('drlItem1');" />
</td>
</tr>
</table>
javascript
<script language="javascript" type="text/javascript">
function findObj(theObj, theDoc) {
var p, i, foundObj;
if (!theDoc) theDoc = document;
if ((p = theObj.indexOf("?")) > 0 && parent.frames.length) {
theDoc = parent.frames[theObj.substring(p + 1)].document;
theObj = theObj.substring(0, p);
}
if (!(foundObj = theDoc[theObj]) && theDoc.all)
foundObj = theDoc.all[theObj];
for (i = 0; !foundObj && i < theDoc.forms.length; i++)
foundObj = theDoc.forms[i][theObj];
for (i = 0; !foundObj && theDoc.layers && i < theDoc.layers.length; i++)
foundObj = findObj(theObj, theDoc.layers[i].document);
if (!foundObj && document.getElementById)
foundObj = document.getElementById(theObj);
return foundObj;
}
function addRow() { //讀取最后一行的行號, 存放在drlTRLastIndex文本框中
var drlTRLastIndex = findObj("drlTRLastIndex", document);
var rowID = parseInt(drlTRLastIndex.value);
var signFrame = findObj("drlFileTable", document);
//添加行
var newTR = signFrame.insertRow(signFrame.rows.length);
newTR.id = "drlItem" + rowID;
//添加文件
var newNameTD = newTR.insertCell(0);
//添加列內容
newNameTD.innerHTML = "<input name='file' type='file' />";
//添加描述
var newNameTD = newTR.insertCell(1);
//添加列內容
newNameTD.innerHTML = "<input style=\"width:256px;\" name='description' type='text'/>";
//添加列:刪除按鈕
var newDeleteTD = newTR.insertCell(2);
//添加列內容
newDeleteTD.innerHTML = "<input id='txtDel" + rowID + "' type='button' value='刪除' onclick=\"delRow('drlItem" + rowID + "');\" />";
//將行號推進下一行
drlTRLastIndex.value = (rowID + 1).toString();
}
//刪除指定行
function delRow(rowid) {
var signFrame = findObj("drlFileTable", document);
var signItem = findObj(rowid, document);
//獲取將要刪除的行的Index
var rowIndex = signItem.rowIndex;
//刪除指定Index的行
signFrame.deleteRow(rowIndex);
}
</script>