Select2 open dropdown on focus

前端 未结 16 2220
陌清茗
陌清茗 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条回答
  •  死守一世寂寞
    2020-12-05 00:55

    I tried these solutions with the select2 version 3.4.8 and found that when you do blur, the select2 triggers first select2-close then select2-focus and then select2-blur, so at the end we end up reopening forever the select2.

    Then, my solution is this one:

    $('#elemId').on('select2-focus', function(){
        var select2 = $(this).data('select2');
        if( $(this).data('select2-closed') ){
            $(this).data('select2-closed', false)
            return
        }
        if (!select2.opened()) {
            select2.open()
        }
    }).on('select2-close', function(){
        $(this).data('select2-closed', true)
    })
    

提交回复
热议问题