且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何检测用户何时忽略 jquery 自动完成建议

更新时间:2021-10-13 07:54:06

我遇到了非常有趣的答案.它处理 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.