简体   繁体   English

去掉 <tr> 我附加到 <tbody>

[英]Remove <tr>s that I appended to a <tbody>

I'm creating a page that basically saves website bookmarks. 我正在创建一个基本上保存网站书签的页面。 It's very basic, but what I'm trying to achieve is this: 这是非常基本的,但是我想要实现的是:

On page load it calls a function called loadPages() . 在页面加载时,它将调用一个名为loadPages()的函数。 The function sends an ajax request backend and retrieves a json_encoded array of websites, with url & title. 该函数发送ajax请求后端,并使用url和title检索网站的json_encoded数组。

When I call the loadPages() function, at the start of it I want to remove any previously appended <tr> s from the <tbody> but nothing I have tried has actually worked and I am lost. 当我调用loadPages()函数时,在它的开头我想从<tbody>删除任何以前附加的<tr> ,但是我尝试过的任何方法都没有真正起作用,我迷路了。 I've spent the last 20 minutes or so browsing SO and google for an answer and none of the solutions have worked for me. 我花了大约20分钟的时间浏览SO和google的答案,但没有一个解决方案对我有用。

The idea, ultimately, is for me to be able to call this function more than once, and it'd just overwrite the table's contents each time. 最终,我的想法是能够多次调用此函数,并且每次都只是覆盖表的内容。

Here is my code: 这是我的代码:

HTML 的HTML

    <table class="table table-striped" id="pagesTable">
        <thead>
            <tr>
                <th class="col-sm-10">Page Name</th>
                <th class="col-sm-2"></th>
            </tr>
        </thead>
        <tbody>

        </tbody>
    </table>

Jquery jQuery的

function loadPages(){

    // Code to remove the previously appended <tr>s from the <tbody> should be here

    $.ajax({
        url: "index.php?page=getAllPages",
        type: "GET",
        dataType: "json",
        success: function(ret){
            $.each(ret, function(){
                $('#pagesTable > tbody').append(
                    '<tr><td class="col-sm-10"><a href="' + this.url + '">' + this.title + '</a></td>'
                    + '<td class="col-sm-2"><button class="btn btn-danger" data-type="remove" data-id="' + this.id + '">Remove</button></td></tr>'
                );
            })
        }
    });
}

Things I have tried: 我尝试过的事情:

$("#pagesTable > tbody").children().remove();

$("#pagesTable > tbody tr").remove();

$("#pagesTable > tbody").empty();

$("#pagesTable > tbody").html("");

Assigning classes to the tbody and tr s, and executing .remove() on the class name like so: $(".class").remove(); 将类分配给tbodytr ,并在类名上执行.remove() ,如下所示: $(".class").remove();

I appreciate any help, thanks. 感谢您的帮助,谢谢。

Inside the success block of your ajax query use the following code before appending: 在您的ajax查询的成功块内,在追加之前使用以下代码:

$("#pagesTable tbody tr").remove();

Don't forget to upvote if you find this helpful!! 如果您觉得有帮助,别忘了投票!!

I think solution should be 我认为解决方案应该是

function loadPages(){

    // Code to remove the previously appended <tr>s from the <tbody> should be here

    $.ajax({
        url: "index.php?page=getAllPages",
        type: "GET",
        dataType: "json",
        success: function(ret){
            $('#pagesTable > tbody').html("");
            $.each(ret, function(){
                $('#pagesTable > tbody').append(
                    '<tr><td class="col-sm-10"><a href="' + this.url + '">' + this.title + '</a></td>'
                    + '<td class="col-sm-2"><button class="btn btn-danger" data-type="remove" data-id="' + this.id + '">Remove</button></td></tr>'
                );
            })
        }
    });
}

$("#pagesTable>tbody").html("")将起作用,请尝试在ajax请求的成功回调开始时将其添加。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM