我在使用jquery ui autocomplete创建combobox时遇到了奇怪的问题。每当我点击滚动条以滚动结果列表,然后再点击我的combobox按钮关闭结果时,结果列表会关闭,然后再次打开。我希望它能关闭菜单。
CSS(强制长结果菜单滚动)
复现步骤
- 打开jsfiddle演示
- 在自动完成框中键入“i”或点击下拉按钮
- 点击垂直滚动条以滚动结果
- 点击下拉按钮
创建按钮的脚本
this.button = $("<button type='button'> </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;
}
我的解决方案是即使焦点转移到小部件本身而不是输入元素,也可以关闭小部件。
有什么想法可以修改这个代码以使其行为如此?