我有一个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