简体   繁体   English

jQuery自动完成可滚动下拉列表

[英]jQuery Autocomplete Scrollable Dropdown

I am working with a jQuery autocomplete textbox. 我正在使用jQuery自动完成文本框。 My code and script are working fine, but now I need a 'scrollable dropdown(along X and Y axis)' instead of a plain dropdown list. 我的代码和脚本工作正常,但现在我需要一个'可滚动的下拉列表(沿着X轴和Y轴)而不是一个简单的下拉列表。

Script used to facilitate autocomplete dropdown: 用于促进自动完成下拉列表的脚本:

$(function () {
    $("#TextBox").autocomplete({
        source: "home/search",
        dataType: 'json',
        minLength: 1,
        // max: 10,
        // scroll:true
    });
});

You can set the x and/or y axis to scrollable, but it's not done through the autocomplete script, it's done through the autocomplete style. 可以将x和/或y轴设置为可滚动,但不是通过自动完成脚本完成,而是通过自动完成样式完成。 The script is not responsible for how elements are displayed, only how they interact. 该脚本不对元素的显示方式负责,只对它们如何交互负责。 The particular style you're looking to change is the ui-autocomplete class. 您希望更改的特定样式是ui-autocomplete类。

Let's say that you want an at-most 200px high and 250px wide drop down (which allows it to be smaller if there are not enough results to fill it all). 假设你想要最多200px高和250px宽的下拉(如果没有足够的结果来填充它,它可以更小)。 Then you would add this to your CSS: 然后你将它添加到你的CSS:

.ui-autocomplete { 
    /* these sets the height and width */
    max-height:200px; 
    max-width: 236px; 

    /* these make it scroll for anything outside */
    overflow-x:scroll;
    overflow-y:scroll;
}

If you want to change the size, simply change the sizes above. 如果您想更改尺寸,只需更改上面的尺寸即可。 If you want to make the x axis not scrollable, but keep the y, then you would change it to: 如果要使x轴不可滚动,但保留y,则将其更改为:

.ui-autocomplete { 
    /* same as above */
    max-height:200px; 
    max-width: 236px; 

    /* note that x is hidden now */
    overflow-x:hidden;
    overflow-y:scroll;
}  

You can read more on the jQuery UI autocomplete site . 您可以在jQuery UI自动完成站点上阅读更多内容。 I've linked to the "Scrollable results" section on the right, and you can click 'view source' to see a full example. 我已链接到右侧的“可滚动结果”部分,您可以点击“查看来源”查看完整示例。 They make a good note that IE does not support max-height, so you'll have to set just a height for IE support. 他们很好地注意到IE不支持max-height,所以你必须为IE支持设置一个高度。

If anyone stuck with same problem (wants autoComplete with vertical scroll) use this: 如果有人遇到同样的问题(想要使用垂直滚动自动完成),请使用:

.ui-autocomplete 
{
  height: 200px;
  overflow-y: auto;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM