【问题标题】:select2 - Always dropdown and not dropupselect2 - 总是下拉而不是下拉
【发布时间】:2015-09-01 09:57:49
【问题描述】:

如何强制 select2 总是下拉而不是下拉?

我尝试了这个解决方案,但它不起作用:This doesn't work

$("#e1").select2()
.on('select2-open', function() {

// however much room you determine you need to prevent jumping
        var requireHeight = 600;
        var viewportBottom = $(window).scrollTop() + $(window).height();

        // figure out if we need to make changes
        if (viewportBottom < requireHeight) 
        {           
            // determine how much padding we should add (via marginBottom)
            var marginBottom = requireHeight - viewportBottom;

            // adding padding so we can scroll down
            $(".aLwrElmntOrCntntWrppr").css("marginBottom", marginBottom + "px");

            // animate to just above the select2, now with plenty of room below
            $('html, body').animate({
                scrollTop: $("#e1").offset().top - 10
            }, 1000);
        }
    });

jsFiddle

【问题讨论】:

    标签: jquery jquery-select2


    【解决方案1】:

    这对我有用:

    $.fn.select2.amd.require([
      'select2/selection/multiple',
      'select2/selection/search',
      'select2/dropdown',
      'select2/dropdown/attachContainer',
      'select2/dropdown/closeOnSelect',
      'select2/utils'
    ], function (MultipleSelection, Search, Dropdown, AttachContainer, CloseOnSelect, Utils) {
      var SelectionAdapter = Utils.Decorate(
        MultipleSelection,
        Search
      );
    
      var DropdownAdapter = Utils.Decorate(
        Utils.Decorate(
          Dropdown,
          CloseOnSelect
        ),
        AttachContainer
      );
    
      $('#e1').select2({
        dropdownAdapter: DropdownAdapter
      });
    });
    

    jsFiddle

    【讨论】:

    • 是这个永久解决方案,我将此代码放在 select2.js 文件或我的 js 文件中
    • @j.code 我发现最简单的方法是将每个要求定义为变量,例如var Utils = $.fn.select2.amd.require('select2/utils'); 然后全局创建DropdownAdapter。这让我可以在任何需要的地方添加dropdownAdapter: DropdownAdapter。希望对您有所帮助。
    • 对于其他好奇的人,SelectionAdapter 旨在像 selectionAdapter: SelectionAdapter 一样在 select2 调用中使用。但是,我认为此解决方案不需要它。
    【解决方案2】:

    我正在使用 Select2 v4.0.5(缩小版)

    打开 select2.js

    搜索c?!j&amp;&amp;k&amp;&amp;c&amp;&amp;(e="below"):e="above"

    要始终下拉,请将"above" 替换为"below"

    要始终退出,请将"below" 替换为"above"

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-11
      • 1970-01-01
      • 2023-03-27
      • 2014-05-13
      • 1970-01-01
      • 2021-10-14
      相关资源
      最近更新 更多