Focus next input once reaching maxlength value

后端 未结 9 1163
梦毁少年i
梦毁少年i 2020-11-29 05:20

How can I focus the next input once the previous input has reached its maxlength value?

a: 
b: 

        
9条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-29 05:31

    You can use plain JavaScript:

    See DEMO.

    Check the character length with el.value.length. If it is equal to the maximum value, move to the next field by using focus(). Bind this function to the keyup event with onkeyup so that the function fires every time after the user keys in a character.

    var a = document.getElementById("a"),
        b = document.getElementById("b"),
        c = document.getElementById("c");
    
    a.onkeyup = function() {
        if (this.value.length === parseInt(this.attributes["maxlength"].value)) {
            b.focus();
        }
    }
    
    b.onkeyup = function() {
        if (this.value.length === parseInt(this.attributes["maxlength"].value)) {
            c.focus();
        }
    }
    

提交回复
热议问题