每次提交新的搜索表单时删除表行

Removing table rows each time a new search form is submitted

本文关键字:表单 删除 搜索表 搜索 提交      更新时间:2023-09-26

我有以下ajax请求:

if (response.success) 
  {
    $('#search-body').remove('tr');
    $('#search-results').show();
    $.each(response.data, function( index, item ) {
    $('#search-body').append('<tr><td>' + item.name + '</td><td>' + item.barcode + '</td><td>' + item.serial + '</tr>');
  });
}

行总是附加到表体,而不是删除现有的行,然后再次执行搜索。

在附加项目之前清除HTML。。。

if (response.success) 
{
    $('#search-body').remove('tr');
    $('#search-results').show();
    $('#search-body').empty();//This will clear the existing elements
    $.each(response.data, function( index, item ) {
        $('#search-body').append('<tr><td>' + item.name + '</td><td>' + item.barcode + '</td><td>' + item.serial + '</tr>');
    });
}

您可以使用.html()而不是.append():

$('#search-body').html('<tr><td>' + item.name + '</td><td>' + item.barcode + '</td><td>' + item.serial + '</tr>');