简体   繁体   English

是否有任何灵活但简单的自动完成JavaScript代码?

[英]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: 我对代码有一些基本要求:

  1. I want to see only those words that start from the typed symbols (not those words that contain the types substring somewhere in the middle). 我只想看那些从键入的符号开始的单词(而不是那些在中间某处包含子字符串类型的单词)。
  2. I want to be able to set the maximal number of words in the drop-dawn menu. 我希望能够在下拉菜单中设置最大单词数。
  3. I want to be able to set style of the drop-dawn menu (color of the background, color of typed substring, color of rest of the word). 我希望能够设置下拉菜单的样式(背景颜色,键入的子字符串的颜色,单词其余部分的颜色)。
  4. Suggested words has to be requested from the web-server (I do not want to load all possible values at once, since it is going to be a huge array of values). 必须从Web服务器请求建议的单词(我不想一次加载所有可能的值,因为它将是一大堆值)。
  5. There is just one example-file that I can just copy to my server and see that it works (then I can start to modify it to put there my content and set my style). 只有一个示例文件,我可以将其复制到服务器上并查看其工作原理(然后可以开始对其进行修改,以放置我的内容并设置我的样式)。

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插件可提供出色的自动完成功能。

jQuery UI 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: 文档说您可以指定以下内容作为源:

  • an Array with local data 具有本地数据的数组
  • a String, specifying a URL 一个字符串,指定一个URL
  • a Callback 回调

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM