javascript - Jquery UI 自动完成组合框按钮单击事件

标签 javascript jquery jquery-ui combobox jquery-autocomplete

我在使用 jquery ui autocomplete when using it to create a combobox 时遇到了奇怪的行为.每当我单击滚动条滚动结果列表然后单击我的组合框按钮关闭结果时,结果列表将关闭然后再次打开。我希望它能关闭菜单。

重现步骤

  1. open jsfiddle demo
  2. 在自动完成中输入“i”或点击下拉按钮。
  3. 点击垂直滚动滚动结果
  4. 点击下拉按钮

创建按钮的脚本

 this.button = $("<button type='button'>&nbsp;</button>")
    .attr({ "tabIndex": -1, "title": "Show all items" })
    .insertAfter(input)
    .button({
         icons: {
             primary: "ui-icon-triangle-1-s"
         },
         text: false
    })
    .removeClass("ui-corner-all")
    .addClass("ui-corner-right ui-button-icon")
    .click(function () {

        // when i put breakpoint here, and my focus is not on input, 
        // then this if steatment is false????

        if (input.autocomplete("widget").is(":visible")) {
            input.autocomplete("close");
            return;
        }

        // work around a bug (likely same cause as #5265)
        $(this).blur();

        // pass empty string as value to search for, displaying all results
        input.autocomplete("search", "");
        input.focus();
});

CSS(强制长结果菜单滚动)

.ui-autocomplete {
    max-height: 100px;
    overflow-y: auto;
    /* prevent horizontal scrollbar */
    overflow-x: hidden;
    /* add padding to account for vertical scrollbar */
    padding-right: 20px;
}
/* IE 6 doesn't support max-height
 * we use height instead, but this forces the menu to always be this tall
 */
* html .ui-autocomplete {
    height: 100px;
}

我的解决方案可能是关闭小部件,即使焦点转移到小部件本身而不是输入元素?

有什么想法可以修改此代码以使其以这种方式运行吗?

最佳答案

基于自动完成小部件的各种单击和鼠标事件的问题,我想到了这个: jsFiddle example .

jQuery:

var input = $('#txtComplete');

var data = [];
var isOpen = false;

function _init() {
    for (var idx = 0; idx <= 100; idx++) {
        data.push('item: ' + idx);
    };
    input.autocomplete({
        source: data,
        minLength: 0,
        open: function(event, ui) {
            isOpen = true;
        },
        select: function(event, ui) {
            isOpen = false;
        }
    });
}

function afterInit() {
    var button = $("<button type='button'>&nbsp;</button>").attr("tabIndex", -1).attr("title", "Show all items").insertAfter(input).button({
        icons: {
            primary: "ui-icon-triangle-1-s"
        },
        text: false
    }).removeClass("ui-corner-all").addClass("ui-corner-right ui-button-icon").click(function(event) {
        input.focus();
        if (isOpen) {
            input.autocomplete("close");
            isOpen = false;
        } else {
            input.autocomplete("search", "");
            event.stopImmediatePropagation();
        }
    });
}
$(window).click(function() {
    input.autocomplete("close");
    isOpen = false;
});
$(function() {
    _init();
    afterInit();
});​

关于javascript - Jquery UI 自动完成组合框按钮单击事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10227171/

相关文章:

java - Android Web View 中的本地存储有时不起作用

jquery - IE9 动画期间的阴影伪像

jquery - 使用 jQuery 获取类列表

c# - 将焦点设置在屏幕顶部 jquery

javascript - jQuery 对话框不关闭

javascript - 需要帮助来了解Javascript关闭

javascript - 使用 firefox 在 mac 上右键单击 flash 应用程序会触发 mousedown

javascript - 使用窗口的主滚动条在其自己的 div 中滚动内容

javascript - 使用悬停动画调整大小的图像不适合容器

javascript - 如何同时高亮主导航栏和侧边导航栏?