Prevent select2 from autmatically focussing its search-input when dropdown is opened

后端 未结 10 443
野性不改
野性不改 2021-01-11 16:02

I\'m looking for a way to prevent select2\'s search-input being automatically focussed when the select2-dropdown is opened. I know this is select2\'s intended default behavi

10条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-11 16:43

    If you want to disable the searchbox and also the auto focus as a text input, e.g. preventing ios browsers to scroll-in the keyboard, use this code:

    $('select').select2({});
    // will remove the searchbox and focus initially
    $(".select2-search, .select2-focusser").remove();
    // will remove the searchbox and focus on selection/close
    $('select').on('select2:closing', function (e) {
      $(".select2-search, .select2-focusser").remove();
    });
    

提交回复
热议问题