我如何对多个单词使用 JQuery 自动完成功能
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/824144/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do i use JQuery Autocomplete for multiple words
提问by Karthik
I have an autocomplete field and am just wondering how can i use it for multiple words? When i type the first word it works perfect, but when i space and type in the second word, its sends the two words back to the server and of course the results are wrong!
我有一个自动完成字段,我只是想知道如何将它用于多个单词?当我输入第一个单词时它完美无缺,但是当我空格并输入第二个单词时,它将两个单词发送回服务器,当然结果是错误的!
eg. when i type the two words,
例如。当我输入这两个词时
'Java javascript'
'Java javascript'
the first word 'Java', autocomplete works well, pull the list.
第一个词“Java”,自动完成效果很好,拉列表。
but when i space and type javascript, autocomplete sends 'Java +javascript' to my ajax function.
但是当我空格并键入 javascript 时,自动完成将“Java + javascript”发送到我的 ajax 函数。
Any ideas how to fix this?
任何想法如何解决这一问题?
采纳答案by Liorsion
Looks like you can't, the built in implementation doesn't support it - you can see in the link above: "is not part of the stable release or distribution".
看起来你不能,内置实现不支持它 - 你可以在上面的链接中看到:“不是稳定版本或发行版的一部分”。
However, I've downloaded and use the external version, available here:
但是,我已经下载并使用了外部版本,可在此处获得:
http://bassistance.de/jquery-plugins/jquery-plugin-autocomplete/
http://bassistance.de/jquery-plugins/jquery-plugin-autocomplete/
it's a little bigger, but the usage is exactly the same, you don't have to change anything, for multiple selection, add to the options {multiple:true}
稍微大了点,但是用法完全一样,什么都不用改,多选的话,加到选项中{multiple:true}
Update
更新
JQueryUI has now been updated for the newest versions making this answer obsolete. An uptodate example can be found here
JQueryUI 现在已针对最新版本进行了更新,因此此答案已过时。可以在此处找到最新示例
回答by Sam
If you are using the "bassistance" version of Autocomplete, it's built in. But if you are using the jQuery UI version, there is an example here:
如果您使用的是“bassistance”版本的 Autocomplete,它是内置的。但是如果您使用的是 jQuery UI 版本,这里有一个示例:
回答by Rama Vadakattu
$("#tagnames").autocomplete("/tags/filter", {
max: 6,
highlightItem: true,
multiple: true,
multipleSeparator: " ",
})
To enable auto complete for multiple words you should set options multiple : trueand multipleSeparator: " "
要为多个单词启用自动完成,您应该设置选项multiple : true和multipleSeparator: " "
回答by Rafael Sabino
Adding a comment for a more complete answer, let's say you have two possible selections with a common prefix:
添加评论以获得更完整的答案,假设您有两个可能的选择,并带有公共前缀:
ip5, ip5 battery
ip5, ip5 电池
if the user types in the textbox ip5, the first selection above will automatically get chosen without letting the user keep typing for the second selection, to avoid this behavior, make sure you use the option
如果用户在文本框 ip5 中键入,上面的第一个选项将自动被选中,而不会让用户继续键入第二个选项,为避免这种行为,请确保使用该选项
triggerSelectOnValidInput: true,
triggerSelectOnValidInput: 真,