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

后端 未结 10 447
野性不改
野性不改 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:48

    Sometimes select2 "steals" focus from other elements. After messing around for quite a bit, I just used this solution below.

    At the very end of the event handler for the YourSelect2.on('change', function(){

    setTimeout(firstInputFocus, 300);
    

    }

    function firstInputFocus() {

        $("YourSelect2").focus();
    

    }

    By setting this slight delay it works. I am able to change focus away from the dropdown. Following the "change" event for select2, it does something internal to the select2 code which prevents you from IMMEDIATELY changing focus. Inserting this slight delay did the trick for me at any rate.

提交回复
热议问题