focusing on next input (jquery)

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

    After searching and developing I end up in a crossbrowser snippet which makes it possible to focus the next input field with same class depending on maxlength (tested with 1 character) also with the ability to focus back via backspace button:

    Javascript (jquery):

    var codeCharInput = 'input.code-char';
    $(codeCharInput+':first').focus();
    $(codeCharInput).keyup(function(e) {
      if ((e.which == 8 || e.which == 46)) {
        $(this).prev(codeCharInput).focus().val($(this).prev().val());
      } else {
        if (this.value.length == this.maxLength) {
          $(this).next(codeCharInput).focus();
        }
      }
    });
    

    HTML:

    
    
    
    
    

提交回复
热议问题