2017-06-20 17 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   " 

を与えるあなたは.text().trim()することができます比較するには、たとえば:

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>

関連する問題