focusing on next input (jquery)

前端 未结 12 2419
慢半拍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:28

    Building on @Vega's answer

    inputs.keydown(function(e) {
        var keys = [8, 9, /*16, 17, 18,*/ 19, 20, 27, 33, 34, 35, 36, 37, 38, 39, 40, 45, 46, 144, 145];
    
        $(this).val('');
    
        if (e.which == 8 && this.value.length == 0) {
            $(this).prev(inputs).focus();
        } else if ($.inArray(e.which, keys) >= 0) {
            return true;
        } else if (this.value.length > charLimit) {
            $(this).next(inputs).focus();
            return false;
        } else if (e.shiftKey || e.which <= 48 || e.which >= 58) {
            return false;
        }
    }).keyup (function () {
        if (this.value.length >= charLimit && $(this).next(inputs).attr('type') === 'text') {
            $(this).next(inputs).focus();
            return false;
        }
    });
    

    If a user clicks on an input that already has a value it will override it, instead of going to the next input, it will also only focus on text inputs. I had a situation where I had a submit input next to the text inputs and if using backspace could accidentally redirect the page.

提交回复
热议问题