Select2 open dropdown on focus

前端 未结 16 2264
陌清茗
陌清茗 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:46

    I've tried a pretty ugly solution but it fixed my problem.

        var tabPressed = false;
    
        $(document).keydown(function (e) {
            // Listening tab button.
            if (e.which == 9) {
                tabPressed = true;
            }
        });
    
        $(document).on('focus', '.select2', function() {
            if (tabPressed) {
                tabPressed = false;
                $(this).siblings('select').select2('open');
            }
        });
    

提交回复
热议问题