在模糊时隐藏自动完成 AJAX 控件的建议列表
我正在实现一个 AJAX“自动建议”框:用户在输入文本字段中键入一个字符串,显示一个带有表格的隐藏 div,然后他/她可以单击一行或使用向上/向下箭头滚动列表;与此同时,焦点仍然保留在输入文本字段上。
一切工作大部分都很好,但有一个细节我无法实现,这在概念上似乎很困难甚至是不可能的。当用户将光标移动到另一个输入字段或仅单击窗口的空白点时,我想隐藏建议列表。这本身并不难实现,我只是添加了一个回调OnBlur;但这会破坏项目的选择OnClick,因为onblur事件在点击之前触发,然后DIV在onclick事件触发之前消失......
我想关于在整个窗口上实现 onclick 回调,然后检查单击发生的位置,但这似乎有点太尴尬和扭曲。有人有更好的主意吗?谢谢!
I am implementing an AJAX "autosuggest" box: the user types a string in an input text field, a hidden div with a table is shown, and then he/she can click on a row or scroll the list with the up/down arrows; in the meantime, the focus is still kept on the input text field.
Everything works mostly fine, but there is a detail which I am not able to implement, which seems conceptually difficult or even impossible. I would like to hide the suggestion list when the user moves the cursor to another input field, or just clicks on an empty point of the window. This is not difficult to achieve by itself, I just added a calback OnBlur; but this breaks the selection of the item OnClick, since the onblur event is triggered before the click, and then the DIV disappears before the onclick event gets triggered...
I thought about implementing an onclick callback on the whole window, and then check where the click occurred, but this seems a bit too awkward and contorted. Does anybody have a better idea? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我正在研究同样的问题并提出了以下解决方案。自动建议列表隐藏了文档的 onclick,这也适用于 IE(window.onclick 不适用于 IE)。
I was working on the same issue and came up with following solution. Autosuggest list made hidden onclick of document which also works in IE(window.onclick does not works in IE).
我遇到了一个类似的问题,但想出了一些不同的解决方案:
希望这有帮助
I had a simular problem but came up with a bit of a different solution:
Hope this helps
对于那些使用 jquery 的人来说,
focusout
事件触发器非常适合此操作:For those working with jquery the
focusout
event trigger works perfectly for this:我目前正在尝试解决同样的问题。
部分解决方案:
使用函数在清除自动建议 DIV 之前等待一小会儿。
但是,此解决方案并不优雅,并且不适用于所有浏览器。
在查看一些流行的自动建议框时,我还没有遇到一个当您单击外部时使自动建议信息消失的框,而是它们通常会超时并消失。
祝你好运。
I am currently trying to solve the same problem.
Partial solution:
Use a function to wait a fraction of a second before you clear the autosuggest DIV
However, this solution is not elegant and it doesn't work in all browsers.
In looking at some of the popular autosuggest boxes, I have not come across one that makes the autosuggest info go away when you click outside, rather they usually time out and disappear.
Best of luck.