Focus next input once reaching maxlength value

后端 未结 9 1165
梦毁少年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:43

    Updated btevfik code, Onkeyup or onkeydown will create issue as you won't be able to delete the previous input on tab navigation. It will be tough to edit or change the text inside the input box as it will be limited to maxlength. So we can use oninput event to achieve the task.

    DEMO

    HTML

    • a:
    • b:
    • c:
    • d:
    • e:
    • f:
    • g:

    Javascript

    $(document).ready(function(){
        $('input').on("input", function(){
            if($(this).val().length==$(this).attr("maxlength")){
                $(this).next().focus();
            }
        });
    });
    

    CSS

    ul {list-style-type:none;}
    li {padding:5px 5px;}
    

提交回复
热议问题