单击外部时 JQuery 自动完成关闭选项 [英] JQuery Autocomplete close option when click outside

查看:22
本文介绍了单击外部时 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.

谢谢

推荐答案

只需关闭对话框关闭时的自动完成:

Just close the autocomplete when the dialog is closed:

$("#dialog").dialog({
    close: function() {
        $('#tags').autocomplete('close');
    }
});

查看实际操作:http://jsfiddle.net/william/3Yz9f/1/.

更新

这取决于您所说的一般"是什么意思.JavaScript 是非常面向事件的.因此,最初,您希望在关闭对话框时关闭自动完成功能,因此是答案的第一部分.当然您可以将它绑定到一些间接事件,例如自动完成模糊或隐藏(您可能需要为隐藏执行自定义事件),但这会给您带来一些风险,它们可能不会被触发,因为它们是间接的.

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.

现在您希望在拖动对话框时关闭它;嗯,这也不难;您可以使用对话框的 dragStart 事件来实现这一点,但它们是两个不同的事件,都在对话框上,而不是自动完成.拖动对话框时,我在自动完成小部件本身上看不到任何间接事件.

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.

如果您的问题是通过 ID 引用自动完成小部件,您可以使用基于上下文的选择器,例如在对话框的事件处理程序中使用 $('.ui-autocomplete-input', this) 而不是 $('#tags').

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.

这篇关于单击外部时 JQuery 自动完成关闭选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆