dongxuanchao1425 2017-05-11 22:18
浏览 195
已采纳

如何在html表中动态添加行

I want to add rows dynamically in a HTML table and the number of the column should be fixed. After adding rows I want to input some text values in each row and column. After entering the text values I want to store it in a database table. How to do that? Here is my code

    function addRow() {
        var root = document.getElementById('mytab').getElementsByTagName('tbody')[0];
        var rows = root.getElementsByTagName('tr');
        var clone = cloneEl(rows[rows.length - 1]);
        root.appendChild(clone);
    }
    function addColumn() {
        var rows = document.getElementById('mytab').getElementsByTagName('tr'), i = 0, r, c, clone;
        while (r = rows[i++]) {
            c = r.getElementsByTagName('td');
            clone = cloneEl(c[c.length - 1]);
            c[0].parentNode.appendChild(clone);
        }
    }
    function cloneEl(el) {
        var clo = el.cloneNode(true);
        return clo;
    }
#mytab td {
  width: 100px;
  height: 20px;
  background: #cccccc;
}
<html>
<head>
    <title>Untitled Document</title>
</head>
<body>
<form action="">
    <input type="button" value="Add a Row" onclick="addRow()">
    <input type="button" value="Add a Column" onclick="addColumn()">
</form>
<br>

<table id="mytab" border="1" cellspacing="0" cellpadding="0">
    <tr>
        <td><input type="text" name="enter1"></td>
        <td><input type="text" name="enter2"></td>
    </tr>

</table>
</body>
</html> 

In this code, the problem is when I enter some text and after that, I add rows or column the same text appears in the newly added rows and column. But I want to enter different text in different boxes and I want the no of the column should be fixed to say no of the column should be 5.

</div>
  • 写回答

1条回答 默认 最新

  • dosi8657 2017-05-11 22:37
    关注

    Updated

    Hi, you have to clear input values after cloning elements, as below. cleanUpInputs(clone); will check cloned DOM and will remove input values. Check the code below.

    function addRow() {
      var root = document.getElementById('mytab').getElementsByTagName('tbody')[0];
      var rows = root.getElementsByTagName('tr');
      var clone = cloneEl(rows[rows.length - 1]);
      cleanUpInputs(clone);
      root.appendChild(clone);
    }
    function addColumn() {
      var rows = document.getElementById('mytab').getElementsByTagName('tr'), i = 0, r, c, clone;
        while (r = rows[i++]) {
          c = r.getElementsByTagName('td');
          clone = cloneEl(c[c.length - 1]);
          cleanUpInputs(clone);
          c[0].parentNode.appendChild(clone);
        }
    }
    function cloneEl(el) {
      var clo = el.cloneNode(true);
      return clo;
    }
    
    function cleanUpInputs(obj) {
      for (var i = 0; n = obj.childNodes[i]; ++i) {
        if (n.childNodes && n.tagName != 'INPUT') {
          cleanUpInputs(n);
        } else if (n.tagName == 'INPUT' && n.type == 'text') {
          n.value = '';
        }
      }  
    }
    #mytab td {
    width: 100px;
    height: 20px;
    background: #cccccc;
    }
    <html>
    <head>
        <title>Untitled Document</title>
    </head>
    <body>
    <form action="">
        <input type="button" value="Add a Row" onclick="addRow()">
        <input type="button" value="Add a Column" onclick="addColumn()">
    </form>
    <br>
    
    <table id="mytab" border="1" cellspacing="0" cellpadding="0">
        <tr>
            <td><input type="text" name="enter1"></td>
            <td><input type="text" name="enter2"></td>
        </tr>
    
    </table>
    </body>
    </html>

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求chat4.0解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果