weixin_33701617 2015-03-08 03:46 采纳率: 0%
浏览 50

如何删除html表中的行?

I want to refresh the table but every time the setinterval active the append insert the same rows again, i want that those rows get removed for the new ones

    $(document).ready(function() 
{ 

    function actualizar()
    {
    $.ajax({
        type: "GET",
        url: "editinplace.php?tabla=1"
    })
    //Vector
    .done(function(json)  
    {
        json = $.parseJSON(json)
        for(var i=0;i<json.length;i++)
        {
            $('.editinplace').append
            (
                "<tr><td class='id'>"
                +json[i].id
                +"</td><td>"
                +json[i].nombre
                +"</td><td>"
                +json[i].apellidos
                +"</td><td>"
                +json[i].telefono
                +"</span></td><td class='editable' data-campo='status'><span>"
                +json[i].status
                +"</span></td></tr>");
        }
        //
    });


    } 
    setInterval(actualizar, 6000);
  • 写回答

1条回答 默认 最新

  • weixin_33725270 2015-03-08 03:57
    关注

    .append will, as the name suggests, append content.

    Either do something like $('.editinplace tr').remove(); to remove all the existing rows before appending, or replace .editinplace entirely:

    $('.editinplace').replaceWith("<table class='editinplace'>"
    +  "<tr><td class='id'>"
    ...
    + "</table>");
    

    See http://api.jquery.com/remove/ and http://api.jquery.com/replacewith/ for more info.

    Ref comments, for testing the existence, I'd add a data attribute to the rows so that you could search for them easily. E.g. replace "<tr><td class='id'>" with "<tr data-id='" + json[i].id + "'><td class='id'>".

    Then in your for() loop, simply check if it exists yet:

    if ($('.editinplace tr[data-id="' + json[i].id + '"]').length == 0) {
        // append
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 用C语言输入方程怎么
  • ¥15 网站显示不安全连接问题
  • ¥15 github训练的模型参数无法下载
  • ¥15 51单片机显示器问题
  • ¥20 关于#qt#的问题:Qt代码的移植问题
  • ¥50 求图像处理的matlab方案
  • ¥50 winform中使用edge的Kiosk模式
  • ¥15 关于#python#的问题:功能监听网页
  • ¥15 怎么让wx群机器人发送音乐
  • ¥15 fesafe材料库问题