How do I move focus to next input with jQuery?

后端 未结 15 1751
不思量自难忘°
不思量自难忘° 2020-12-04 09:32

I am using the autocomplete plugin with jQuery and it is working fine. However, in IE, when the user selects an item in the autocomplete, the focus does not then move to the

相关标签:
15条回答
  • 2020-12-04 10:10

    Use eq to get to specific element.

    Documentation about index

    $("input").keyup(function () {
       var index = $(this).index("input");		 	
       $("input:eq(" + (index +1) + ")").focus(); 
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    
    <input type="text" maxlength="1"  />
    <input type="text" maxlength="1"  />
    
    <input type="text" maxlength="1"  />
    
    <input type="text" maxlength="1"  />
    <input type="text" maxlength="1"  />
    <input type="text" maxlength="1"  />

    0 讨论(0)
  • 2020-12-04 10:11
    onchange="$('select')[$('select').index(this)+1].focus()"
    

    This may work if your next field is another select.

    0 讨论(0)
  • 2020-12-04 10:14

    What Sam meant was :

    $('#myInput').focus(function(){
        $(this).next('input').focus();
    })
    
    0 讨论(0)
  • 2020-12-04 10:15

    The easiest way is to remove it from the tab index all together:

    $('#control').find('input[readonly]').each(function () {
        $(this).attr('tabindex', '-1');
    });
    

    I already use this on a couple of forms.

    0 讨论(0)
  • 2020-12-04 10:16

    You can do something like this:

    $("input").change(function() {
      var inputs = $(this).closest('form').find(':input');
      inputs.eq( inputs.index(this)+ 1 ).focus();
    });
    

    The other answers posted here may not work for you since they depend on the next input being the very next sibling element, which often isn't the case. This approach goes up to the form and searches for the next input type element.

    0 讨论(0)
  • 2020-12-04 10:17

    Could you post some of your HTML as an example?

    In the mean-time, try this:

    $('#myInput').result(function(){
        $(this).next('input').focus();
    })
    

    That's untested, so it'll probably need some tweaking.

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