简体   繁体   English

jQuery-删除所有行,如果该行的第一个td没有具有给定值的id

[英]Jquery-Delete all the row if first td of row does not have the id with given value

I have a table with many rows. 我的桌子上有很多行。 from which i want to delete all the rows except the row, of which first td has a given id. 从中,我想删除除第一行具有给定ID的行以外的所有行。

i am in half way 我半途而废

 <tr>
   <td id="1" class="td_link" onclick="viewPointDetails(1)">Barrackpur</td>
   <td class="td_link" id="11" class="edit_client" onclick="editPoint(1)">India</td>
 </tr>


 <tr>
   <td id="2" class="td_link" onclick="viewPointDetails(2)">Madiwala</td>
   <td class="td_link" id="11" class="edit_client" onclick="editPoint(1)">India</td>
 </tr>

and my jquery; 和我的jQuery;

   $('tr').each(function () {   
       if first td does not have given  id say 1  then delete this row.
   });  

my First row is header. 我的第一行是标题。 so i dont want this to be checked(do not want this row to be deleted). 因此,我不希望对此进行检查(不希望此行被删除)。

You should be able to achieve this without the each loop: 您应该能够在没有each循环的情况下实现这一目标:

$("tr:not(:has(#1))").remove();

This uses the :has selector to return rows which contain an element matching the selector. 这使用:has选择器返回包含与选择器匹配的元素的行。 It uses the :not selector to get the opposite of that (so you end up with all rows that do not contain an element matching the selector), and then uses remove to remove all elements in the matched set. 它使用:not选择器得到相反的结果(因此您最终得到的所有行都不包含与选择器匹配的元素),然后使用remove删除匹配集中的所有元素。

Try this 尝试这个

$(document).ready(function () {
     $('tr').each(function () {   
      if($(this).find("td:first").attr("id") == 2)
      {
        $(this).remove();
      }
  });
});

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

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