jQuery focus not working in Chrome

前端 未结 4 1987
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-17 08:24

Please see this fiddle: http://jsfiddle.net/yg49k/

The following code works fine in FireFox but doesn\'t work in the latest version of Chrome.

HTML:

相关标签:
4条回答
  • 2020-12-17 08:27

    I got the same problem and got it fixed using the following code:
    You should also have a name attribute for your input to get this working.

    $("#one").on("input", null, null, function() {  
       if($("#one").val().length == 2) {  
           setTimeout(function() { $('input[name="one"]').focus() }, 3000);  
       }  
    });  
    

    Hope this helps someone who did not get it working using the other suggestions.

    0 讨论(0)
  • 2020-12-17 08:30

    You should use:

    $("#one").on("keyup paste",function() {
        console.log($(this).val());
        if($(this).val().length == 2) { $("#two").focus(); }
    });
    

    DEMO

    And the same for the #five handler.

    The oninput event seems to have the same behaviour as onkeypress.

    0 讨论(0)
  • 2020-12-17 08:35
    $("#one").keydown(function(){
       if($(this).val().length==2)
       {
          $("#two").focus();
       }
    });
    
    0 讨论(0)
  • 2020-12-17 08:47

    Seems like a bug in Chrome. Sometimes it is too fast to execute events properly;)

    Found a workaround http://jsfiddle.net/Rd2rZ/

    $("#one").on("input", function() {
        if($("#one").val().length == 2) { 
            setTimeout(function(){
                $("#two").focus();
            }, 1);
        }
    });
    

    Use setTimeout with a minimal delay. It will slow down Chrome and make it work.

    0 讨论(0)
提交回复
热议问题