使用jquery ui 自动完成创建组合框时,我遇到了奇怪的行为。每当我单击滚动条滚动浏览结果列表,然后单击我的组合框按钮关闭结果时,结果列表就会关闭,然后再次打开。我希望它关闭菜单。
复制步骤
- 打开 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;
}
即使焦点转移到小部件本身而不是输入元素,我的解决方案也可能关闭小部件?
任何想法如何修改此代码使其行为如此?