美文网首页
JavaScript-给table动态加行,删除行

JavaScript-给table动态加行,删除行

作者: 阿历Ali | 来源:发表于2017-11-28 19:29 被阅读0次

本文非原创,在网上找到了可行的方法,做记录。

通过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>

相关文章

网友评论

      本文标题:JavaScript-给table动态加行,删除行

      本文链接:https://www.haomeiwen.com/subject/bkiebxtx.html