在模糊时隐藏自动完成 AJAX 控件的建议列表

发布于 2024-11-05 08:41:12 字数 381 浏览 1 评论 0原文

我正在实现一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

我不会写诗 2024-11-12 08:41:12

我正在研究同样的问题并提出了以下解决方案。自动建议列表隐藏了文档的 onclick,这也适用于 IE(window.onclick 不适用于 IE)。

document.onclick = function(ev) 
{
    this.hideAutosuggestListDiv();
};

this.hideAutosuggestListDiv = function()
{
    this.div.style.display = 'none';
};

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).

document.onclick = function(ev) 
{
    this.hideAutosuggestListDiv();
};

this.hideAutosuggestListDiv = function()
{
    this.div.style.display = 'none';
};
浮世清欢 2024-11-12 08:41:12

我遇到了一个类似的问题,但想出了一些不同的解决方案:

document.onclick = closeSuggest;
function closeSuggest() {
  document.getElementById('suggestions').style.display = "none";
}

function catchTAB(event) {
  if(event.keyCode ==9) {
    closeSuggest();
    document.getElementById('ELEMENT').focus(); //the element that is currently focused
  }
}

希望这有帮助

I had a simular problem but came up with a bit of a different solution:

document.onclick = closeSuggest;
function closeSuggest() {
  document.getElementById('suggestions').style.display = "none";
}

function catchTAB(event) {
  if(event.keyCode ==9) {
    closeSuggest();
    document.getElementById('ELEMENT').focus(); //the element that is currently focused
  }
}

Hope this helps

雾里花 2024-11-12 08:41:12

对于那些使用 jquery 的人来说,focusout 事件触发器非常适合此操作:

$('#input-box').focusout(function() {
    $('#suggestions').fadeOut(); 
});

For those working with jquery the focusout event trigger works perfectly for this:

$('#input-box').focusout(function() {
    $('#suggestions').fadeOut(); 
});
木格 2024-11-12 08:41:12

我目前正在尝试解决同样的问题。

部分解决方案:

使用函数在清除自动建议 DIV 之前等待一小会儿。

function pause(milliseconds) {
        var dt = new Date();
        while ((new Date()) - dt <= milliseconds) {  }
}

onBlur="pause(500);clearAutosuggestBox()"

但是,此解决方案并不优雅,并且不适用于所有浏览器。

在查看一些流行的自动建议框时,我还没有遇到一个当您单击外部时使自动建议信息消失的框,而是它们通常会超时并消失。

祝你好运。

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

function pause(milliseconds) {
        var dt = new Date();
        while ((new Date()) - dt <= milliseconds) {  }
}

onBlur="pause(500);clearAutosuggestBox()"

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文