PHP/Javascript - 即时搜索,但不同?
我有一个带有空白搜索栏的页面,然后在表格下方有一堆联系人。每个联系人都是一个 div。
我希望能够在搜索栏中输入文本时过滤联系人表。 (例如,如果在搜索栏中输入“Fran”,您只会看到姓名中包含“Fran”的联系人。如果删除“Fran”,所有联系人都会恢复为默认值。)
这可能吗?怎么做?(我找到了一个即时搜索DIY指南,但它只能按照Google的方式工作,在搜索栏下方有一个下拉菜单。)
I have a page with a blank search bar, and then a bunch on contacts below it in a table. Each contact is one div.
I want to be able to filter the contact table as text is entered into the search bar. (So, for instance, if "Fran" was typed in to the search bar, you'd only see contacts with "Fran" in their name. And then all would go back to default if "Fran" was deleted.)
Is this possible? How? (I found an instant search diy guide, but it only worked the way Google does, with a dropdown beneath the search bar.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我建议使用这个jquery插件,它不需要ajax或服务器通信,它只是过滤每次击键时根据输入框内容呈现的表格。
确保在您的项目中还包含 jquery 库以及此插件才能正常工作。
I would suggest using this jquery plugin, it requires no ajax or server communication, it simply filters a rendered table according to an input box contents at every keystroke.
Make sure to also include the jquery library in your project along with this plugin for this to work.
您可以尝试使用 jQuery 自动完成插件 来实现相同的效果
You can try jQuery auto complete plugin for the same
以下包含您需要的基本逻辑,尽管它是相反的 - 它隐藏了它找到的内容并且仅用于完全匹配。另外,这是一个按键事件,因此它只会在您输入名称后按空格键后隐藏 div,因此您也需要对其进行调整。
要在长文本字符串中查找匹配项,您需要使用一些正则表达式来测试搜索字符串。
The following contains the basic logic you need although it's the reverse - it hides content that it finds and only for exact matches. Also, it's an on keypress event so it only hides the divs after you hit space after typing the name so you'll need to tweak that too.
To look for matches within a long string of text, you will need to use some regex to test against the search string.
下面是一些 Javascript 示例:
Here is some example Javascript for you: