duandui5648 2019-02-15 02:45
浏览 109
已采纳

附加ajax调用表不清除jquery php中的先前数据?

I am working with jquery and php to get dynamic data with ajax call into table format. I have one select option where i can choose different companies.Onselect i will get some data of particular employee with ajax i am binding that data to table. But problem is if i choose company 1 then i can able to bind data of company 1. onchange company 2 data of company 1 should be disappear and company 2's data only be there. but in my condition onchange company 2 previous data is still there.

Below is my ajax call code:

 $.ajax({

          method: "GET",
           dataType: 'json',
           url:"getdata.php?id="+emp_id,
              success:function (response){
                     $.each(response, function( index, value ) {

                              $("table.table").append("<tr><td>" + response.emp_name + "</td><td>"  + "</td><td><input type='file'></td></tr>");

                      });
              },  
      });

Below is my html:

<table class="table">
        <thead>
        <tr>
            <th>Employee Name</th>
            <th></th>
        </tr>
        </thead>
        <tbody>

        </tbody>
</table>
  • 写回答

2条回答 默认 最新

  • dongmian5325 2019-02-15 02:50
    关注

    since you use append on success of getting data, you must clear the data on your table or else it will only add data below.

    ON YOUR HTML

    Set attr id to your <tbody> tag:

     <tbody = "bodytable">
    

    ON YOUR SCRIPT

    add code to clear the data on your <tbody>

              $("bodytable").empty();
    
     $.each(response, function( index, value ) {
    
          $("table.table").append("<tr><td>" + response.emp_name + "</td><td>"  + "</td><td><input type='file'></td></tr>");
    
          });
    

    There are many ways to do that. Hope this helps!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 在若依框架下实现人脸识别
  • ¥15 网络科学导论,网络控制
  • ¥100 安卓tv程序连接SQLSERVER2008问题
  • ¥15 利用Sentinel-2和Landsat8做一个水库的长时序NDVI的对比,为什么Snetinel-2计算的结果最小值特别小,而Lansat8就很平均
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同