ReplaceWith()添加新行,但不删除旧行?

时间:2019-05-06 01:05:49

标签: javascript jquery html

我正在尝试在jQuery中使用replaceWith()替换表中的一行,但并未替换。添加了新行,而旧行仍然存在(直到刷新页面)。我必须缺少一些东西,因为从文档中应该可以...

表格HTML:

<div class="table-responsive-md table-hover">
    <table class="table table-striped table-sm" id="setup-table">
        <thead>
            <tr>
                <th scope="col">Report</th>
                <!-- <th scope="col">Category</th> -->
                <th scope="col">Interval</th>
                <th scope="col">Timing</th>
                <th scope="col">Enabled</th>
                <th scope="col"></th>
            </tr>
        </thead>
        <tbody>
            <div class="row text-center" style="display:flex; flex-wrap:wrap;">
                <% user.reports.forEach(function(report) { %>
                    <tr>
                        <th scope="row" id="table-report"><%= report.report %></th>
                        <!-- <td id="table-category"><%= report.category %></td> -->
                        <td id="table-interval"><%= report.interval %></td>
                        <td id="table-timing">@<%= report.timing %></td>
                        <td id="table-enabled">
                            <div class="custom-control custom-switch">
                                <!-- <input type="checkbox" class="custom-control-input on-disabled-switch" disabled id="revenue_switch">
                                <label class="custom-control-label" for="revenue_switch"></label> -->

                                <% if (report.enabled) { %>
                                    <input class="form-check-input" type="checkbox" value="" checked disabled id="table-check">
                                    <label class="form-check-label" for="table-check"></label>
                                <% } else { %>
                                    <input class="form-check-input" type="checkbox" value="" disabled id="table-check">
                                    <label class="form-check-label" for="table-check"></label>
                                <% } %>
                            </div>
                        </td>
                        <td>
                            <button type="button" class="btn btn-link edit-button" data-toggle="modal" data-target="#edit-modal">
                                <i class="fas fa-pencil-alt"></i>
                            </button>
                        </td>
                    </tr>
                <% }); %>
            </div>
        </tbody>
    </table>
</div>

用新行替换一行的JavaScript,我有一个模式,当提交时,该代码将运行:

var row = 
    '<tr>' +
        '<th scope="row" id="table-report">' + data.report + '</th>' +
        '<td id="table-interval">' + data.interval + '</td>' +
        '<td id="table-timing">@' + data.timing + '</td>' +
        '<td id="table-enabled">' +
            '<div class="custom-control custom-switch">' +
                '<input class="form-check-input" type="checkbox" value="" disabled ' + checked + ' id="table-check">' +
                '<label class="form-check-label" for="table-check"></label>' +
            '</div>' +
        '</td>' +
        '<td>' +
            '<button type="button" class="btn btn-link edit-button" data-toggle="modal" data-target="#edit-modal">' +
                '<i class="fas fa-pencil-alt"></i>' +
            '</button>' +
        '</td>' +
    '</tr>';

$("tr").eq(rowNum).replaceWith(row);

我从用户单击表中一行上的编辑按钮时获得rowNum:

rowNum = ($(this).index());

我知道rowNum具有正确的值,但无法弄清楚为什么不替换该行,只是添加了新行。

预先感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

$("tr").eq(rowNum).replaceWith(row);函数将删除具有行内容的索引rowNum

我将其复制并且运行良好,您可以在源代码中进一步检查细节。

function replace(){
let data = { report : "A", interval: 100, timing: 15};
let checked = false;
var row = 
    '<tr>' +
        '<th scope="row" id="table-report">' + data.report + '</th>' +
        '<td id="table-interval">' + data.interval + '</td>' +
        '<td id="table-timing">' + data.timing + '</td>' +
        '<td id="table-enabled">' +
            '<div class="custom-control custom-switch">' +
                '<input class="form-check-input" type="checkbox" value="" disabled ' + checked + ' id="table-check">' +
                '<label class="form-check-label" for="table-check"></label>' +
            '</div>' +
        '</td>' +
        '<td>' +
            '<button type="button" class="btn btn-link edit-button" data-toggle="modal" data-target="#edit-modal">' +
                '<i class="fas fa-pencil-alt"></i>' +
            '</button>' +
        '</td>' +
    '</tr>';

let rowNum = 1;
$("tr").eq(rowNum).replaceWith(row);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.8.1/css/all.css" />
<div class="table-responsive-md table-hover">
    <table class="table table-striped table-sm" id="setup-table">
        <thead>
            <tr>
                <th scope="col">Report</th>
                <!-- <th scope="col">Category</th> -->
                <th scope="col">Interval</th>
                <th scope="col">Timing</th>
                <th scope="col">Enabled</th>
                <th scope="col"></th>
            </tr>
        </thead>
        <tbody>
            <div class="row text-center" style="display:flex; flex-wrap:wrap;">
               
                    <tr>
                        <th scope="row" id="table-report">report</th>
                       
                        <td id="table-interval">interval</td>
                        <td id="table-timing">timing</td>
                        <td id="table-enabled">
                            <div class="custom-control custom-switch">
                                

                               
                                    <input class="form-check-input" type="checkbox" value="" checked disabled id="table-check">
                                    <label class="form-check-label" for="table-check"></label>
                               
                            </div>
                        </td>
                        <td>
                            <button type="button" class="btn btn-link edit-button" data-toggle="modal" data-target="#edit-modal">
                                <i class="fas fa-pencil-alt"></i>
                            </button>
                        </td>
                    </tr>
                    
                    <tr>
                        <th scope="row" id="table-report">report</th>
                       
                        <td id="table-interval">interval</td>
                        <td id="table-timing">timing</td>
                        <td id="table-enabled">
                            <div class="custom-control custom-switch">
                                

                               
                                    <input class="form-check-input" type="checkbox" value="" checked disabled id="table-check">
                                    <label class="form-check-label" for="table-check"></label>
                               
                            </div>
                        </td>
                        <td>
                            <button type="button" class="btn btn-link edit-button" data-toggle="modal" data-target="#edit-modal">
                                <i class="fas fa-pencil-alt"></i>
                            </button>
                        </td>
                    </tr>
                
            </div>
        </tbody>
    </table>
</div>

<button onclick="replace()">Replace</button>