jquery 根据完全匹配显示行
jquery show row based on exact match
所以我试图根据完全匹配显示几行(不想只包含因为 21 和 12 都匹配“1”)。
这里是 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 中的条件永远不会 returns 为真,即使有多个完全匹配。
如果您检查 .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>
所以我试图根据完全匹配显示几行(不想只包含因为 21 和 12 都匹配“1”)。
这里是 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 中的条件永远不会 returns 为真,即使有多个完全匹配。
如果您检查 .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>