Focus next input once reaching maxlength value

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

    If you are adding input text fields dynamically then you can try this.

    This will re-inject the script into the DOM and works Perfectly.

    $('body').on('keyup', '#num_1',function(){
      if (this.value.length === parseInt(this.attributes["maxlength"].value)) {
        $('#num_2').focus();
      }
    })
    $('body').on('keyup','#num_2', function(){
       if (this.value.length === parseInt(this.attributes["maxlength"].value)) {
        $('#num_3').focus();
      }
    })
    <input type="text" class="form-control" name="number" maxlength="3" id="num_1">
    <input type="text" class="form-control" name="number" maxlength="3" id="num_2">
    <input type="text" class="form-control" name="number" maxlength="4" id="num_3">

    0 讨论(0)
  • 2020-11-29 05:51

    Verified Answer have one issue which focus previous field if previous field have valid length

    I have Modified Above Answer to fix complete length of previous tag

    var container = document.getElementsByClassName("container")[0];
        container.onkeyup = function(e) {
        var target = e.srcElement || e.target;
        var maxLength = parseInt(target.attributes["maxlength"].value, 10);
        var myLength = target.value.length;
        if (myLength >= maxLength) {
            var next = target;
            while (next = next.nextElementSibling) {
                if (next == null)
                    break;
                if (next.tagName.toLowerCase() === "input") {
                    next.focus();
                    break;
                }
            }
        }
        // Move to previous field if empty (user pressed backspace)
        else if (myLength === 0) {
             var previous = target;
           // Move to previous field if backspace is pressed
            if (code == 8) {
                previous = previous.previousElementSibling;
                if (previous != null) {
                    if (previous.tagName.toLowerCase() === "input") {
                        previous.focus();
                    }
                }
            } else {
                while (previous = previous.previousElementSibling) {
                    if (previous == null)
                        break;
                    if (previous.tagName.toLowerCase() === "input") {
                        var mLength = parseInt(previous.attributes["maxlength"].value, 10);
                        var pMyLength = previous.value.length;
                        // Move to previous field if it does not have required length
                        if (mLength == pMyLength) {
                            break;
                        } else {
                            previous.focus();
                            break;
                        }
                    }
                }
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-29 05:54

    if you are going to have many fields you can do something like this.

    basically on keyup get the length of the input and then compare it to the maxlength, if matches, then focus onto the next input field.

    http://jsfiddle.net/btevfik/DVxDA/

    $(document).ready(function(){
        $('input').keyup(function(){
            if(this.value.length==$(this).attr("maxlength")){
                $(this).next().focus();
            }
        });
    });
    
    0 讨论(0)
提交回复
热议问题