Moving a focus when the input text field reaches a max length

前端 未结 8 1430
北恋
北恋 2020-12-01 03:54

I do have a credit card number form. The number is divided into four parts just as on a real credit card.

I want to add a JavaScript taste to the form where when a u

8条回答
  •  有刺的猬
    2020-12-01 04:30

    I haven't tested it, but I think this will work. It will probably also move the focus to the button when the 4th field is completed.

    $("form input").change(function () {
        var maxLength = $(this).attr('maxlength');
    
        if($(this).val().length == maxLength) {
            $(this).next().focus();
        }
    }
    

提交回复
热议问题