2017-06-20 51 views
0

所以我試圖根據完全匹配顯示幾行(不想僅僅是包含因爲21和12都匹配'1')。基於完全匹​​配的jquery顯示行

這裏是JavaScript:

$("tr:has(.logType:contains('" + curr + "'))").filter(function() { 
    return $(this).text == curr; 
}).removeAttr("style"); 

和HTML:

<table> 
    <thead> 
     <tr> 
      ... 
      <th class="logType"> 
       Log Type 
       <!-- some options --> 
       </select> 
      </th> 
    <thead> 
    <tbody> 
      ... 
      <tr> 
      <td class="logType"> 
       <!-- some content --> 
      </td> 
      </tr> 
    </tbody> 

目前jQuery的內部條件從不返回true,即使有多個準確匹配。

回答

1

如果你檢查什麼.text()是給你的,你會看到,它不會匹配,因爲它包含了所有的空格:

console.log($(this).text()) 

"   Log Type   " 

可以.trim().text()比較,例如:

var curr = "abc" 
 

 
var items = $("tr:has(.logType:contains('" + curr + "'))").filter(function() { 
 
    console.log($(this).text()) 
 
    console.log($(this).text().length) // > 3 
 
    return $(this).text().trim() == curr; 
 
}) 
 

 
alert(items.length)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<table> 
 
    <thead> 
 
    <tr> 
 

 
     <th class="logType"> 
 
     Log Type 
 
     <!-- some options --> 
 
     </th> 
 
    </tr> 
 
    <thead> 
 
     <tbody> 
 

 
     <tr> 
 
      <td class="logType"> 
 
      abc 
 
      <!-- some content --> 
 
      </td> 
 
     </tr> 
 
     </tbody> 
 
</table>