Tab through elements using Enter key including select2

喜夏-厌秋 提交于 2020-04-18 06:12:49

问题


I am using following code to tab through form elements using enter key. Problem is that this code skip select2 elements.

        $('body').on('keydown', 'input, select', function(e) {
            if (e.key === "Enter") {
                var self = $(this), form = self.parents('form:eq(0)'), focusable, next;
                focusable = form.find('input,a,select,button,textarea').filter(':not([disabled]):not([tabindex="-1"]):visible');
                next = focusable.eq(focusable.index(this)+1);
                if (next.length) {
                    next.focus();
                } else {
                    //form.submit();
                }
                return false;
            }
        });

回答1:


Change your keydown to keyup

  $('body').on('keyup', 'input, select', function(e)

Reason is keydown is already handled in select2 library for choosing an item



来源:https://stackoverflow.com/questions/61041573/tab-through-elements-using-enter-key-including-select2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!