jquery - select2 - 始终下拉而不是下拉

标签 jquery jquery-select2

如何强制 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

最佳答案

这对我有用:

$.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

关于jquery - select2 - 始终下拉而不是下拉,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32328691/

相关文章:

javascript - 如何按特定字符对 select2 结果进行排序?

javascript - 使用 javascript 迭代数组

jquery - Chrome 不刷新多选元素

javascript - 尝试使用 Jquery 缓和一个 div

javascript - 选择的 jQuery 插件不起作用。

jquery-select2 - jquery Select2 Ajax - 如何设置值 (initSelection)

javascript - 检查插件是否已在 jQuery 中初始化?

jquery - 下拉菜单位于 select2 下

html - 自动填充在与自动填充无关的 chrome 输入中弹出

javascript - 使用 JS 在对象中选择 #...