javascript - 在 JavaScript 中克隆表格行并删除值

标签 javascript clone user-input

我有一组文本输入字段,当用户单击“添加”按钮时,我希望将其克隆。我的问题是,这些字段是通过用户的输入来克隆的。使用 Javascript,如何重置文本输入字段的值,以便可以在不克隆用户输入文本的情况下克隆它们。

这是我的代码...

<table width="766"  border="0" cellspacing="0" cellpadding="0" id="albumTable">  
    <tr id="clone">  
        <td width="230"><input type="text" name="dj_1" /></td>  
        <td width="230"><input type="text" name="affiliations_1" /></td>  
        <td width="230"><input type="text" name="rating_1" /></td>  
        <td width="230"><input type="text" name="comments_1" /><input type="hidden" name="count" value="1" class="count"/></td>
        <td width="286" style="position:absolute; top:110px; left: 670px;"><input name="button" type="button" id="Add" value="ADD ROW" onclick="addRow(this.parentNode.parentNode)"></td>
    </tr>
</table>

<script type="text/javascript">
    function addRow(r){  
        var root = r.parentNode;  
        var allRows = root.getElementsByTagName('tr');  
        var cRow = allRows[0].cloneNode(true)  
        var cInp = cRow.getElementsByTagName('input');  
        var countRow = cRow.getElementsByClassName('count');  

        for(var i=0;i<cInp.length;i++){
            cInp[i].setAttribute('name',cInp[i].getAttribute('name').replace(/\d/g,(allRows.length+1)))   
        }  
        for(var j=0;j<countRow.length;j++){
            countRow[j].setAttribute('value',countRow[j].getAttribute('value').replace(/\d/g,(allRows.length+1)))
        }  
        root.appendChild(cRow);  
    }  

    function shownames(){
        var allInp=document.getElementsByTagName('input');
        for(var i=0;i<allInp.length;i++){
            alert(allInp[i].name)  
        }  
    }  
</script>

最佳答案

类似这样的事情:

var cln = document.getElementByID("clone");
var clns = cln.getElementsByTagName("td");

for(var i = 0; i <clns.length; i++)
{
   clns[i].innerHTML = "";
}

关于javascript - 在 JavaScript 中克隆表格行并删除值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5509845/

相关文章:

javascript - 可选值组作为函数参数的 typescript

javascript - $ 未在 jsfiddle 中定义

java - HashMap 中克隆方法的输出

java: 克隆方法冲突

android - 在 Android 中处理用户输入事件的基础是什么?

c - 你如何居中对齐N行文本

javascript - 为什么我的“全部删除”按钮需要双击才能起作用?

javascript - 延迟加载:How to display multiple pdf documents as one with pdf.js?

javascript - JQuery 扩展字符串

python - 在 Python 中验证用户输入的正确方法