我有一個jQuery的功能,搜索表中的單詞。例如在jQuery中表多個單詞搜索
表
<table>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Band Name</th>
</tr>
<tr>
<td>John</td>
<td>Lennon</td>
<td>Beatles</td>
</tr>
<tr>
<td>Paul</td>
<td>McCartney</td>
<td>Beatles</td>
</tr>
<tr>
<td>George</td>
<td>Harrison</td>
<td>Beatles</td>
</tr>
<tr>
<td>Ringo</td>
<td>Starr</td>
<td>Beatles</td>
</tr>
現在。我有一個輸入文本框,如果你根據表格輸入任何單詞,例如Paul ,結果將是隻有保羅·麥卡特尼的表格。所有其他TD元素將被隱藏。
$(document).ready(function(){
if (!RegExp.escape) {
RegExp.escape = function (s) {
return s.replace(/[\-\[\]{}()*+?.,\\\^$|#\s]/g, "\\$&")
};
}
///search this table
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/D6nzC/7/
哇!你太棒了。 O.O謝謝! :D – Vincent
我們遇到了一些問題。如果我們有三列,該怎麼辦?第三列是樂隊名稱:beatles – Vincent
,如果我這樣搜索。 「beatles john」 – Vincent