Select2 open dropdown on focus

前端 未结 16 2218
陌清茗
陌清茗 2020-12-05 00:21

I have a form with multiple text inputs and some select2 elements. Using the keyboard to tab between fields works fine - the Select2 element behaves like a form element and

16条回答
  •  Happy的楠姐
    2020-12-05 00:53

    The problem is, that the internal focus event is not transformed to jQuery event, so I've modified the plugin and added the focus event to the EventRelay on line 2063 of Select2 4.0.3:

    EventRelay.prototype.bind = function (decorated, container, $container) {
        var self = this;
        var relayEvents = [
          'open', 'opening',
          'close', 'closing',
          'select', 'selecting',
          'unselect', 'unselecting',
          'focus'
        ];
    

    Then it is enough to open the select2 when the focus occurs:

    $('#select2').on('select2:focus', function(evt){
        $(this).select2('open');
    });
    

    Works well on Chrome 54, IE 11, FF 49, Opera 40

提交回复
热议问题