我有一个过滤器函数,需要检查一个元素数组与输入字段的值。我原本使用this solution,但它重新评估全部结果当输入第二个或第三个术语。循环顺序通过一个数组与另一个数组与javascript
这是我的情况Fiddle。
目前,如果我输入'鱼',它将只返回包含'鱼'的行,到目前为止这么好。如果我还输入'yellow',那么'fish yellow',它会返回'Fish'和'Parrot',但实际上我只希望它只返回'Fish',因为它是唯一包含全部条款输入字段。的
所以不是过滤这样的:
input value = "Term1 Term2 Term3"
- 起租1,再次开始,然后
- 词条2,重新开始,然后
- TERM3,重新开始...
有没有办法像这样过滤'jo'数组:
- 起租1,然后
- 起租1和词条2,然后
- 起租1和词条2和TERM3等?
下面,我试图推动在输入字段中的单词数到另一个数组,然后试图通过所有的话得到它的循环顺序,但它不工作:(
HTML
<input id="filter-field" placeholder="Search" type="text" />
<table cellspacing="0" cellpadding="0" >
<thead>
<tr>
<th>Pet</th>
<th>Colour</th>
</tr>
</thead>
<tbody>
<tr>
<td>cat</td>
<td>white</td>
</tr>
<tr>
<td>dog</td>
<td>black</td>
</tr>
<tr>
<td>parrot</td>
<td>yellow</td>
</tr>
<tr>
<td>fish</td>
<td>yellow</td>
</tr>
<tr>
<td>hamster</td>
<td>black</td>
</tr>
</tbody>
</table>
jQuery的
$("#filter-field").on('input', function() {
// Split the current value of searchInput
var data = this.value.split(" "),
inputLength = $("#filter-field").val().length;
// Create a jquery object of the rows
var jo = $("table > tbody").find("tr");
if (this.value == "") {
jo.show();
return;
}
// Hide all the rows
jo.hide();
jo.filter(function() {
var $t = $(this),
textField = $('#filter-field').val().split(" "),
wordCount = textField.length - 1, // Get length of array, then subtract 1 so 'word #1' matches 'position 0' in array
wordCounter = [];
// I wanted this to end up as [1, 2, 3, 4 etc] to somehow check each word against 'jo', but each .push() simply adds 1 to the first array value
wordCounter.push(wordCount);
// I want this to check for 'word A', then if I enter further words, 'word A and word B' etc
if ($t.is(":contains('" + textField[wordCounter] + "')")) {
return true;
} else {
return false;
}
}).show();
});
在_“Term1和Term2和Term3等?”,是r要显示所有的“Term1和Term2以及Term3等?” ? – guest271314 2015-03-19 05:53:25
是的,要求仅显示包含所有术语的结果,而不仅仅显示输入的最后一个术语 – Timmah 2015-03-19 05:59:35
显示包含匹配术语的整个“tr”或仅包含“td”? “td”是否隐藏起来? – guest271314 2015-03-19 06:01:24