使用jquery删除重复的行数据

时间:2016-01-29 06:57:58

标签: javascript jquery html css

                <head>
                <script src= "http://code.jquery.com/jquery-1.7.min.js" type="text/javascript"></script>

                </head>
                <body>
                <table width="779" border="5" id="test">
                    <tr  class="tablerow">
                        <td>ee</td>    
                        <td>11</td>            
                        <td>test</td>
                        <td>3</td>    
                        <td>15974079
                        </tr>
                    <tr>
                        <td>ww1</td>
                        <td>hi</td>    
                        <td>test2</td>
                        <td>d</td>    
                        <td>15859779    
                     </tr>
                     <tr  class="tablerow">
                        <td>ww2</td>
                        <td>hi</td>      
                        <td>test2</td>
                        <td>t </td>     
                        <td>15974386</td>
                      </tr>
                    <tr>
                        <td>ww2</td>
                        <td>hi</td>      
                        <td>test4</td>
                        <td>e</td>      
                      <td>15974386</td>  
                    </tr>
                    <tr>
                        <td>ww4</td>
                        <td>hi</td>      
                        <td>test5</td>
                        <td>d</td>      
                        <td>15974652</td>
                    </tr>
                    <tr>
                      <td>sssd</td>    
                      <td>fgdg</td>      
                      <td>test6</td>      
                      <td>dzz</td>      
                      <td>15974652</td>
                    </tr>
                    <tr>
                      <td>sssd</td>    
                      <td>d</td>    
                      <td>test7</td>
                      <td>d</td>    
                      <td>15974652</td>
                    </tr>
                    </table>
                <script>
                var arr = $("#test tr");

                $.each(arr, function(i, item) {
                    var currIndex = $("#test tr").eq(i);
                    var matchText = currIndex.children("td").eq(2).text();
                    $(this).nextAll().each(function(i, inItem) {
                        if(matchText===$(this).children("td").eq(2).text()) {
                            $(this).remove();
                        }
                    });
                });
                </script>

                </body>maybe is duplicate question but i tried many links and couldn't get my answer.

我需要删除第三列上的重复行基,而不是第一列 首先使用eq,但它可能不起作用,实际上在非常小的表工作中但是当数字或行和列变大时它不起作用 例如: 当我选择eq(4)或eq(2)时,不显示行甚至不重复 在行&#34; ww4&#34; 我查了一下 但它唯一的工作是第一栏

i tried this one too but that one is check all column in row not only base on one columns i need check only value of third row

                <head>
                <script src= "http://code.jquery.com/jquery-1.7.min.js" type="text/javascript"></script>

                </head>
                <body>
                <table width="779" border="5" id="test">
                    <tr  class="tablerow">
                    <td>ee    
                    <td>11            
                    <td>test
                    <td>    
                    <td>15974079    </tr>
                    <tr>
                    <td>ww1
                    <td>hi    
                    <td>test2
                    <td>    
                    <td>15859779    </tr>
                    <tr  class="tablerow">
                    <td>ww2
                    <td>hi      
                    <td>test2
                    <td>      
                  <td>15974386  </tr>
                    <tr>
                    <td>ww2
                    s<td>hi      
                    <td>test4
                    <td>      
                  <td>15974386  </tr>
                    <tr>
                    <td>ww4
                    <td>hi      
                    <td>test5
                    <td>      
                  <td>15974652  </tr>
                    <tr>
                      <td>sssd    
                      <td>fgdg      
                      <td>test6      
                      <td>dzz      
                      <td>15974652      
                  </tr>
                    <tr>
                      <td>sssd    
                      <td>    
                      <td>test7
                      <td>    
                  <td>15974652</tr>
                    </table>
                <script>
                var arr = $("#test tr");

                $.each(arr, function(i, item) {
                    var currIndex = $("#test tr").eq(i);
                    var matchText = currIndex.children("td").eq(2).text();
                    $(this).nextAll().each(function(i, inItem) {
                        if(matchText===$(this).children("td").eq(2).text()) {
                            $(this).remove();
                        }
                    });
                });
                </script>

                </body>

1 个答案:

答案 0 :(得分:1)

试试这个

var seen = {};
$('table tr').each(function() {
    var txt = $(this).children("td:eq(2)").text();
    if (seen[txt])
        $(this).remove();
    else
        seen[txt] = true;
});

http://jsfiddle.net/VbUxd/440/