我有一個用於查找表中多個單詞的代碼,但它還不完美。 如果我輸入那些多個單詞不按順序怎麼辦?例如「John Lennon 151 sf」 和「paul mccartney 753 tj」,並且我輸入了單詞「lennon 753 tj」,結果應該是其中的兩個。但我收到的結果僅僅是約翰·列儂的行..如何在表中找到所有可能的結果jquery
這裏是我的代碼
<input id="emp_search" />
<input id="search" type="button" value="search" />
<table>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>number</th>
<th>letters</th>
</tr>
<tr>
<td>John</td>
<td>Lennon</td>
<td>151</td>
<td>sf</td>
</tr>
<tr>
<td>Paul</td>
<td>McCartney</td>
<td>753</td>
<td>tj</td>
</tr>
<tr>
<td>George</td>
<td>Harrison</td>
<td>24</td>
<td>ge</td>
</tr>
<tr>
<td>Ringo</td>
<td>Starr</td>
<td>26</td>
<td>hg</td>
</tr>
</table>
,這裏是..
if (!RegExp.escape) {
RegExp.escape = function (s) {
return s.replace(/[\-\[\]{}()*+?.,\\\^$|#\s]/g, "\\$&")
};
}
jQuery(function ($) {
///search this table
$(' #search ').click(function() {
var searchthis = new RegExp($(' #emp_search ').val().replace(/\s+/, '|'), 'i');
$("table").find("tr").slice(1).each(function (index) {
var text = $.trim($(this).text());
$(this).toggle(searchthis.test(text));
});
});
});
,什麼我將添加到我的jQuery?在此先感謝:)
http://jsfiddle.net/wind_chime18/ANLgD/5/
我認爲這是解決在以前的帖子本身http://jsfiddle.net/arunpjohny/D6nzC/10/ –
@arun它不工作時我想搜索一個人。還有樂隊名字披頭士樂隊。比如「John beatles」 – Vincent
@Vincent如果你搜索「John Beatles」,它會顯示所有的行爲「Beatles」出現在所有行中http://jsfiddle.net/arunpjohny/D6nzC/15/ –