[英]Is there any flexible but simple auto-complete JavaScript code?
Auto-complete seems to be a standard tool on web sites. 自动完成功能似乎是网站上的标准工具。 Surprisingly, it is hard to find a ready-to-use solution in Internet.
令人惊讶的是,很难在Internet上找到现成的解决方案。 I have some basic requirements to the code:
我对代码有一些基本要求:
Do I want to much or there is something in the Internet? 我想要很多还是互联网上有东西?
ADDED: 添加:
As the first answer suggest, there is a solution provided by JQuery . 作为第一个答案, JQuery提供了一个解决方案 。 I have managed to make it work.
我设法使它起作用。 However it was not trivial, since the code is given out of the context.
但是,这并非无关紧要,因为代码是在上下文之外给出的。 First it is not clear where I need to put this code (head?, body?).
首先,不清楚我需要在哪里放置此代码(head?,body?)。 Then if I put it in the body it start to work only after I put this stuff to the head:
然后,如果将其放在体内,则只有在将这些东西放到头部后,它才能开始工作:
<link rel="stylesheet" href="/css/base.css" type="text/css" media="all" />
<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.7/themes/base/jquery-ui.css" type="text/css" media="all" />
<link rel="stylesheet" href="http://static.jquery.com/ui/css/demo-docs-theme/ui.theme.css" type="text/css" media="all" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js" type="text/javascript"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.7/jquery-ui.min.js" type="text/javascript"></script>
<script src="http://jquery-ui.googlecode.com/svn/tags/latest/external/jquery.bgiframe-2.1.2.js" type="text/javascript"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.7/i18n/jquery-ui-i18n.min.js" type="text/javascript"></script>
<script src="/js/demos.js" type="text/javascript"></script>
<script src="/themeroller/themeswitchertool/" type="text/javascript"></script>
It was not written on the page that I need to use this lines to make the code work. 它不是写在页面上的,我需要使用此行来使代码正常工作。 I copied this lines from the head of the page that provide the example of the auto-complete.
我从页面顶部复制了这些行,这些行提供了自动完成的示例。
Moreover, I still have the problem since the auto-complete search all the words that contain the typed substring (and I need the words that start from the typed substring). 而且,由于自动完成搜索所有包含类型化子字符串的单词(我需要从类型化子字符串开始的单词),所以我仍然遇到问题。
If jQuery is an option as library, the UI plugin offers a great autocomplete. 如果使用jQuery作为库的选项,则UI插件可提供出色的自动完成功能。
How about this demo: 这个演示怎么样:
http://jsbin.com/oniji4 http://jsbin.com/oniji4
Moreover, I still have the problem since the auto-complete search all the words that contain the typed substring (and I need the words that start from the typed substring).
而且,由于自动完成搜索所有包含类型化子字符串的单词(我需要从类型化子字符串开始的单词),所以我仍然遇到问题。
For this you need to implement your own implementation of the source parameter. 为此,您需要实现自己的source参数实现。 Documentation says that you can specify the following as the source:
文档说您可以指定以下内容作为源:
Now that you say that you want the suggested words to come from a server, you should use the second option (specify a URL for JSONP). 现在,您说希望建议的单词来自服务器,您应该使用第二个选项(为JSONP指定URL)。 That allows you to handle "I need the words that start from the typed substring" part.
这样就可以处理“我需要从键入的子字符串开始的单词”部分。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.