【问题标题】:Clicking to add new row in table removing entered data单击以在表格中添加新行删除输入的数据
【发布时间】:2021-08-06 09:44:53
【问题描述】:

我编写了在单击按钮时在表中添加新行的代码。如果我输入值然后单击按钮,则数据将被删除。为什么?

document.getElementById('add').addEventListener('click', function(){
document.querySelector('table tbody').innerHTML += document.querySelector('table tbody tr').innerHTML;
});
<button id="add">Add</button>

<table>
    <thead>
      <tr>
        <th>1</th>
        <th>2</th>
        <th>3</th>
        <th>4</th>
        <th>5</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td><input type="number" placeholder="test" min="1"></td>
        <td><input type="number" placeholder="test" min="1"></td>
        <td><input type="number" placeholder="test" min="1"></td>
        <td><input type="number" placeholder="test" min="1"></td>
        <td><input type="checkbox" checked></td>
      </tr>
    </tbody>
</table>

【问题讨论】:

    标签: javascript html html-table


    【解决方案1】:

    您可能需要考虑使用 append() 方法。

    
    
    <body>
        <button id="add">Add</button>
        <table>
            <thead>
            <tr>
                <th>1</th>
                <th>2</th>
                <th>3</th>
                <th>4</th>
                <th>5</th>
            </tr>
            </thead>
            <tbody id="tableBody">
            <tr>
                <td><input type="number" placeholder="test" min="1"></td>
                <td><input type="number" placeholder="test" min="1"></td>
                <td><input type="number" placeholder="test" min="1"></td>
                <td><input type="number" placeholder="test" min="1"></td>
                <td><input type="checkbox" checked></td>
            </tr>
            </tbody>
        </table>
        <script>
            let count = 0;
    
            function newInputElement(type, placeholder="", min=1) {
                let input = document.createElement('input');
                input.type = type;
                input.id = type + count;
    
                if(type == 'number') {
                    input.placeholder = placeholder;
                    input.min = min;
                } else {
                    console.log("add another type for support");
                }
    
                count++;
                return input.outerHTML;
            }
    
            function addNewRowOfInputs(target, destination, triggerEvent) {
                document.getElementById(target).addEventListener(triggerEvent, function() {
                    let row = document.createElement('tr');
                    
                    row.innerHTML =
                    '<tr>' +
                    '<td>' + newInputElement('number', 'test') + '</td>' +
                    '<td>' + newInputElement('number', 'test') + '</td>' +
                    '<td>' + newInputElement('number', 'test') + '</td>' +
                    '<td>' + newInputElement('number', 'test') + '</td>' +
                    '<td><input type="checkbox" checked></td>' +
                    '<tr>';
    
                    document.getElementById(destination).append(row);
                });
            }
    
            addNewRowOfInputs('add', 'tableBody', 'click');
        </script>
    </body>
    

    通过这样做,您可以添加到现有内容而不覆盖其内容。

    【讨论】:

    • 它有效。谢谢!你知道为什么我的方法不能正常工作吗?
    • 我稍微修改了您的代码,使其更易于使用和重新编写。 row.innerHTML = document.querySelector('table tbody tr').innerHTML;
    • @test,据我所知,你没有做错任何事。从逻辑上讲,我也会这样做。我能想到的最多的是每次按下按钮时都会覆盖你的代码。 (采用默认的 html 结构并复制/粘贴它)在已经编写的代码/输入之上。
    • 不要全局公开变量!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-20
    • 1970-01-01
    • 1970-01-01
    • 2017-09-17
    • 1970-01-01
    • 2021-10-18
    相关资源
    最近更新 更多