简体   繁体   English

jQuery-自动完成插件

[英]jQuery - autocomplete plugin

I'm using this one: 我正在使用这个:

https://github.com/agarzola/jQueryAutocompletePlugin https://github.com/agarzola/jQueryAutocompletePlugin

The only problem is that when you type the 1st character in the input field, it will display all term suggestions, including terms which don't contain this character. 唯一的问题是,当您在输入字段中输入第一个字符时,它将显示所有术语建议,包括不包含该字符的术语。

And only after you type the 2nd character it will behave normally, displaying tag suggestions containing only those 2 characters. 而且只有在您输入第二个字符后,它才能正常工作,显示仅包含这两个字符的标签建议。

I'm using this configuration: 我正在使用此配置:

$('input.autocomplete').autocomplete("localhost/boo/?get_suggestions=1", {
   width: 250,
   max: 100,
   matchContains: true,
   minChars: 1,
   selectFirst: true,
   cache: false,
   multiple: true,
   multipleSeparator: " "
 });

Does anyone know a workaround for this? 有谁知道解决方法?

替代文字

Also when I type a random string, which I know it's not in the list, for eg. 另外,当我输入一个随机字符串时,我知道它不在列表中,例如。 *&@FGBHFHBOFUBF*UB# it will display the entire list again :( *&@FGBHFHBOFUBF*UB# ,它将再次显示整个列表:(

The back-end: 后端:

if($_GET['get_suggestions']):
  $terms = get_all_terms();
  foreach ($terms as $term) echo $term['title']."\n";
  die();
endif;

I would suggest disabling first character suggestions altogether, and start on the second or even third character. 我建议完全禁用第一个字符建议,并从第二个甚至第三个字符开始。 Most people including myself find it annoying to instantly get spammed with suggestions after typing a "b". 包括我在内的大多数人都发现输入“ b”后立即被垃圾邮件困扰很烦人。

Change your minchars to 2 将您的分钟数更改为2

 minChars: 2

That will make it so it only suggests things from the 2 character. 这样就可以做到,它仅建议2个字符中的内容。

or try turning off multiple. 或尝试关闭多个。

multiple: false

Do you need that to be enabled? 您需要启用它吗?

If that's not your cup of tea, post the code for localhost/boo/?get_suggestions=1 and we will take a look see :) 如果那不是您想要的,请发布localhost / boo /?get_suggestions = 1的代码,我们将看看:)

That autocomplete code is wrong at some part. 该自动完成代码在某些方面是错误的。 From the image you posted, when you typed a a long list of all terms showed; 从您发布,当你输入的图像a全方面的一个长长的清单显示; when you typed ac the list of suggestions matched a instead of ac . 当您输入ac时,建议列表匹配a而不是ac

What does it mean? 这是什么意思? the code takes the input value before the new character is taken into account. 该代码将在考虑新字符之前获取输入值。 You may take a dive into the plugin code, or use a new plugin. 您可以深入研究插件代码,也可以使用新的插件。

I'm the one maintaining that github repo. 我是维护那个github回购的人。 Here's what's going on: 这是怎么回事:

In the case of using a url for the data, the script sends the request as whatever you set in the specified url, and adds q=[current input value] at the end. 如果使用url作为数据,脚本将按照您在指定url中设置的内容发送请求,并在末尾添加q=[current input value] In the case of initial load when you type in “a”, this is sent to your backend script: localhost/boo/?get_suggestions=1&q=a . 在初始加载的情况下,键入“ a”时, localhost/boo/?get_suggestions=1&q=a被发送到您的后端脚本: localhost/boo/?get_suggestions=1&q=a Thus, autocompelte.js expects this initial query to produce only items that match the query. 因此,autocompelte.js希望此初始查询仅生成与查询匹配的项目。 After that initial request, the script will take on the filtering subsets internally, to decrease server load. 在该初始请求之后,脚本将在内部承担过滤子集,以减少服务器负载。 This explains why “ac” returns only items that match your criteria. 这解释了为什么“ ac”仅返回符合您条件的项目。 This is the autocomplete script doing its job of filtering what the server gave it. 这是自动完成脚本,负责过滤服务器提供的内容。

If I'm interpreting your backend code correctly, it makes no use whatsoever of the q parameter being sent in the request, so your code is returning every possible term. 如果我正确地解释了您的后端代码,则它不使用请求中发送的q参数,因此您的代码将返回所有可能的项。 Autocomplete presumes this is the result of a proper search and shows you all of it, waiting for more characters to be typed in for it to filter the list further. 自动完成功能假定这是正确搜索的结果,并向您显示所有内容,并等待输入更多字符以供其进一步过滤列表。

The point being that you need to make your backend script filter the list of terms to whatever matches the q parameter before returning it to the autocomplete script. 关键是您需要使后端脚本将术语列表过滤到与q参数匹配的任何值,然后再将其返回到自动完成脚本。

Let me know if I can be of further assistance! 让我知道是否可以提供进一步的帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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