如何检测用户何时忽略 jquery 自动完成建议 [英] How to detect when user ignores jquery autocomplete suggestions
问题描述
我们需要能够在用户输入文本时调用处理程序而无需从一组自动完成建议中进行选择.
We need the ability to call a handler if a user enters text without choosing from a set of autocomplete suggestions.
在我们的例子中,输入的文本是否与任何自动完成值匹配并不重要 - 我们只关心用户是否点击/选择了一个自动完成值,与否.
In our case it doesn't matter if the entered text matches any autocomplete values or not - we only care if the user clicks/selects an autocomplete value, or not.
我们的表单的工作方式是,用户开始输入联系人的姓氏并获得自动完成建议,包括联系人的全名和公司.如果用户选择自动完成建议之一,我们将填充多个字段.一切正常.
The way our form works is that a user starts entering a contact's last-name and gets presented with autocomplete suggestions including the contact's full-name and company. If the user selects one of the autocomplete suggestions, we populate multiple fields. That's all working fine.
新要求是,如果用户没有选择其中一项建议,我们需要清空多个字段 - 我们希望使用从自动完成库调用的处理程序来实现.
The new requirement is that if the user does NOT select one of the suggestions, we need to blank out multiple fields - which we'd like to implement using a handler called from an autocomplete library.
看之前
Looking at previous answers, it doesn't look like we can do this with Jörn Zaefferer's autocomplete plugin, but if you have a patch or some idea how to implement the function, this is the library we'd try and push it into.
推荐答案
我遇到了非常有趣的答案.它处理 jQuery 插件如何管理它们的状态.当我查看 firebug(使用 firequery)时,我发现当我单击显示 DOM 节点(在 HTML 选项卡中)的 jQuery 属性的图标时,它显示了附加到 DOM 节点的所有插件数据,并附加到整个自动完成参数.于是我找到了解决方案.
I have come across very interesting answer. It deals with how jQuery plugins manages their status. As I was looking at firebug(with firequery) I found that when I clicked icon showing jQuery properties of a DOM Node(in HTML tab) it showed all the plugin data attached to a DOM Node and attached to that was entire autocomplete parameters. Thus I found the solution.
$('#test').autocomplete({
change:function( event, ui ) {
var data=$.data(this);//Get plugin data for 'this'
if(data.autocomplete.selectedItem==undefined)
alert("manual");
else
alert("selected");
}
});
自动完成的作用是,如果我们从列表中选择,则 selectedItem 包含我们刚刚选择的 JSON 对象及其所有属性.但是如果我们按 ESC 或取消自动填充,它会被设置为空.
What autocomplete does is that if we select from list then selectedItem contains the JSON object we just selected with all its properties. But if we press ESC or Cancel the autofill it is set to null.
这篇关于如何检测用户何时忽略 jquery 自动完成建议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!