简体   繁体   中英

How to hide TR if its values are similar to another TR in jquery?

I want to hide the TR if its first TD value is similar to another TRs TD value.

Eg:

<table>
<tr>
    <td>123</td>
    <td>Example 1</td>
</tr>
<tr>
    <td>789</td>
    <td>Example 2</td>
</tr> 
<tr>
    <td>123</td>
    <td>Example 3</td>
</tr>   
</table>

What I want:

<table>
<tr>
    <td>123</td>
    <td>Example 1</td>
</tr>
<tr>
    <td>789</td>
    <td>Example 2</td>
</tr>    
</table>

Can anybody tell me how to do this?

Any help would be highly appreciated.

You could create a map of the text in the first TD, and then filter based on that

 var trs = $('table tr'), map = trs.map(function() { return $(this).find('td:first').text().trim() }).get(); trs.filter(function(i) { return map.indexOf( $(this).find('td:first').text().trim() ) !== i; }).hide(); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table> <tr> <td>123</td> <td>Example 1</td> </tr> <tr> <td>789</td> <td>Example 2</td> </tr> <tr> <td>123</td> <td>Example 3</td> </tr> </table> 

HTML

<table id="my_table">
 <tr>
<td>123</td>
<td>Example 1</td>
   </tr>
   <tr>
<td>789</td>
<td>Example 2</td>
 </tr> 
  <tr>
<td>123</td>
<td>Example 3</td>
 </tr>   
 </table>

In javascript using jquery

<script>
$("#my_table tr").each(function(){
 var findTxt=$(this).find("td:eq(0)").text();
 $("#my_table tr").each(function(){
   if($(this).find("td:eq(0)").text()==findTxt){
        $(this).remove();
       }
     });
     });
 </script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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