focusing on next input (jquery)

前端 未结 12 2443
慢半拍i
慢半拍i 2020-11-30 05:09

I\'ve got four inputs that each take one number. What I want to do is set the focus automatically to the next input once the number has been set. They all have the class \"i

12条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-30 05:41

    Here is the code I use for making enter key to behave as tab, i.e, focus to next element when pressing the Enter key or focusing previous element when pressing shift+Enter.

    1) Essentially:

    tabables = $("*[tabindex != '-1']:visible");
    var index = tabables.index(element);
    tabables.eq(index + 1).focus();
    

    2) Here you are a "class" that encapsulates the behaviour, having in mind fordward and backwards and valid focusable elements.

    I hope it helps and if some code suits your needs, feel free to adapt to your needs :)

    EnterAsTab = function () {
        this.ENTER_KEY = 13;
    };
    
    EnterAsTab.prototype.init = function () {
        this.listenOnEnterKey();
    };
    
    EnterAsTab.prototype.listenOnEnterKey = function () {
    
        var me = this;
        $('form input').on('keypress', function (event) {
    
                if (event.which === me.ENTER_KEY) {
    
                    if (!event.shiftKey)
                        me.findNextFocusableElement(this);
                    else
                        me.findPreviousFocusableElement(this);
    
                    event.preventDefault();
                }
            }
        );
    };
    
    EnterAsTab.prototype.findNextFocusableElement = function (element) {
        this.findFocusableElement(element, this.increaseIndex);
    };
    
    EnterAsTab.prototype.findPreviousFocusableElement = function (element) {
        this.findFocusableElement(element, this.decreaseIndex);
    };
    
    EnterAsTab.prototype.findFocusableElement = function (element, callable) {
    
        var tabables = $("*[tabindex != '-1']:visible");
        var index = tabables.index(element);
        var counter = 1;
        var nextElement = undefined;
    
        try {
    
            while (true) {
    
                if ((nextElement = tabables.eq(index + counter)).length === 0) {
                    break;
                }
    
                if (this.isFocusableElement(nextElement)) {
    
                    var newIndex = callable.call(this, index, counter);
                    tabables.eq(newIndex).focus();
    
                    break;
                } else {
                    counter++;
                }
            }
        } catch (error) {
            console.log(error);
        }
    
    };
    
    EnterAsTab.prototype.increaseIndex = function (index, counter) {
        return (index + counter);
    };
    
    EnterAsTab.prototype.decreaseIndex = function (index, counter) {
        return index - counter;
    };
    
    EnterAsTab.prototype.isFocusableElement = function (element) {
    
        return ['SELECT', 'TEXTAREA'].indexOf(element.prop('tagName')) > -1 ||
            element.is(':text') ||
            element.is(':checkbox') ||
            element.is(':radio');
    };
    
    var enterAsTab = new EnterAsTab();
    enterAsTab.init();
    

提交回复
热议问题