单击外部时 JQuery 自动完成关闭选项
我想知道 JQuery 自动完成是否有办法,如果我单击选项框外部进行选择或单击键盘中的 ESCAPE,何时打开选项。它会关闭,而无需选择一个选项。
有人知道正确的方法吗?仍然认为使用某些东西来检查是否聚焦自动完成,如果不关闭它,但这只是一个想法。
谢谢
I would like to know if there's a way in JQuery Autocomplete, when is open the options if I click OUTSIDE the options box to select or click ESCAPE in the keyboard. It closes, without having to select one option.
Anyone know the correct way to do it? Still thought using something to check if focus the autocomplete , if not to close it but is just an a IDEA.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只需关闭对话框关闭时的自动完成功能:
查看实际操作:http: //jsfiddle.net/william/3Yz9f/1/。
更新
这取决于你所说的“一般”是什么意思。 JavaScript 非常面向事件。因此,最初,您希望在关闭对话框时关闭自动完成功能,因此是答案的第一部分。当然,您可以将其绑定到一些间接事件,例如自动完成模糊或隐藏(您可能需要为隐藏执行自定义事件),但这给您带来了一些风险,即它们可能不会被触发,因为它们是间接的。
现在您希望它在拖动对话框时关闭;嗯,这也不难;您可以使用对话框的
dragStart
事件来实现此目的,但它们是两个不同的事件,都在对话框上,而不是自动完成。拖动对话框时,我在自动完成小部件本身上没有看到任何间接事件。如果您的问题是通过 ID 引用自动完成小部件,则可以使用基于上下文的选择器,例如使用
$('.ui-autocomplete-input', this)
而不是$( '#tags')
在对话框的事件处理程序中。Just close the autocomplete when the dialog is closed:
See this in action: http://jsfiddle.net/william/3Yz9f/1/.
Update
It depends what you mean by being "general". JavaScript is very much event-oriented. So, initially, you want autocomplete to close when dialog is closed, hence the first part of the answer. Sure you can bind it to some indirect events, such as autocomplete blur or hide (you may need to do a custom event for the hide), but that gives you a bit of risk that they might not be triggered, as they're indirect.
Now you want it to close when dialog is dragged; well, that's not hard either; you can achieve this with the
dragStart
event for dialog, but they're two different events, both on dialogs, not autocomplete. I don't see any indirect event on autocomplete widget itself when dialog is dragged.If your issue is referring to the autocomplete widget by ID, you could use a context-based selector, e.g. use
$('.ui-autocomplete-input', this)
rather than$('#tags')
in dialog's event handlers.我遇到了同样的问题。您只需要做一件小事。
调用“搜索”方法后,设置焦点。按 Esc 键并在框外单击将关闭下拉菜单。
我正在使用类别子类(http://jqueryui.com/demos/autocomplete/#categories)
所以我的代码看起来
我希望它在 .autocomplete 小部件上也能同样工作。
I ran into the same problem. There is only one little thing you have to do.
After calling the 'search' methdod, set the focus. The Esc and clicking outside the box will close the dropdown.
I'm using the category subclass (http://jqueryui.com/demos/autocomplete/#categories)
so my code looks like
I'm expecting it will work the same on the .autocomplete widget as well.