jQuery 自动完成 UI - 我希望它能够在焦点上开始搜索,而无需用户输入任何内容
jQuery 自动完成 UI - 我想启动“onfocus”搜索,并在用户按选项卡或单击搜索字段时立即显示选择列表,而无需用户输入任何内容。
默认行为似乎要求用户输入一个字符或向下箭头来开始滚动并开始搜索并获取值,即使我将所需的字符数设置为零也是如此。
$( "#contact" ).autocomplete({ source: 'remote.php', minLength: 0 });
谢谢!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
比埃米特的答案复杂一点,但是...
如下所示:
A bit more complicated than Emmett's answer, but...
Here it is:
我发现这段代码更加简洁并且特定于元素。
I found that this code was a little cleaner and element specific.
尝试将
焦点
与自动完成功能绑定。查看我的示例 JSFiddle。
Try binding the
focus
with autocomplete.Check out my sample JSFiddle.
确保自动完成的
minLength
为 0。Make sure your autocomplete's
minLength
is 0.此解决方案对我来说并不完全有效,因为在选择所需结果后,自动完成结果框会再次弹出。这是因为
.focus
方法是在close:
事件之前执行的。此外,根据该答案中的代码,一旦盒子关闭,它就不会再打开,因为
close: 在
.focus
之后执行,>ending 变量保持true
。以下代码解决了这两个问题(请注意,
close:
事件中的变量looking
设置为 false):This solution didn't exactly work for me because the autocomplete results box would pop back up once more after selecting the desired result. This was because the
.focus
method was executed before theclose:
event.Additionally, according to the code in that answer, once the box closed, it wouldn't open back up because the
closing
variable stayedtrue
due toclose:
being executed after.focus
.The following code resolved those two issues (note the variable
closing
is set to false in theclose:
event):这个解决方案对我不起作用,但是这个:
效果很好(来源:jquery 论坛)
this solution not working for me, but this:
works good (source: jquery forum)
JQUERY 实际上建议使用这种方法
http://api.jqueryui.com/autocomplete/#method-search< /a>
基本上你使用 minLength:0 和焦点事件来搜索“”。
JQUERY actually suggests this method
http://api.jqueryui.com/autocomplete/#method-search
basically you use minLength:0 and the focus event with a search for "".