简体   繁体   English

自动聚焦下一个输入字段

[英]Auto focus the next input field

How do you auto focus the next input field? 您如何自动聚焦下一个输入字段?

When all the input fields are placed one after the other like this 这样将所有输入字段一个接一个放置时

<div>
    <input type="number" name="name">
    <input type="number" name="name">
    <input type="number" name="name">
</div>

you can generally just do something like this 你通常可以做这样的事情

this.nextSibling.nextSibling.focus();

However, I have a situation where there is a variable number of <span> tags between each input field. 但是,我遇到的情况是,每个输入字段之间都有可变数量的<span>标记。

Something like 就像是

<div>
    <input type="number" name="name">
    <span></span>
    <span></span>
    <span></span>
    <span></span>
    <input type="number" name="name">
    <span></span>
    <input type="number" name="name">
    <span></span>
    <span></span>
    <input type="number" name="name">
</div>

How do I configure 我该如何配置

this.nextSibling.nextSibling.focus();

to get to the next input sibling? 进入下一个输入兄弟?

You could just keep looping through the nextElementSibling s until you find one that matches the tagName you're looking for: 您可以一直循环遍历nextElementSibling直到找到与您要查找的tagName匹配的对象:

 document.querySelectorAll('input').forEach((input) => { input.oninput = function() { let { nextElementSibling } = this; while (nextElementSibling && nextElementSibling.tagName !== 'INPUT') { nextElementSibling = nextElementSibling.nextElementSibling; } if (nextElementSibling) { nextElementSibling.focus(); } } }); 
 <div> <input type="number" name="name"> <span></span> <span></span> <span></span> <span></span> <input type="number" name="name"> <span></span> <input type="number" name="name"> <span></span> <span></span> <input type="number" name="name"> </div> 

If you want to specify something more specific than a tagName , you can use Element.prototype.matches : 如果要指定比tagName更具体的内容,可以使用Element.prototype.matches

 document.querySelectorAll('input').forEach((input) => { input.oninput = function() { let { nextElementSibling } = this; while (nextElementSibling && !nextElementSibling.matches('input[name="foo"]')) { nextElementSibling = nextElementSibling.nextElementSibling; } if (nextElementSibling) { nextElementSibling.focus(); } } }); 
 <div> <input type="number" name="name"> <span></span> <span></span> <span></span> <span></span> <input type="number" name="foo"> <span></span> <input type="number" name="name"> <span></span> <span></span> <input type="number" name="foo"> </div> 

Note the use of nextElementSibling rather than nextSibling - nextSibling will select adjacent nodes , not just Elements. 注意使用nextElementSibling而不是nextSibling - nextSibling将选择相邻节点 ,而不仅仅是元素。 (for example, nextSibling can evaluate to a text node, which you don't care about.) If you want an element, best to be specific and use nextElementSibling . (例如, nextSibling可以求值为您不关心的文本节点。)如果需要元素,则最好具体化并使用nextElementSibling

 <div> <input type="number" tabindex="1" name="name"> <span></span> <span></span> <span></span> <span></span> <input type="number" tabindex="2" name="name"> <span></span> <input type="number" tabindex="3" name="name"> <span></span> <span></span> <input type="number" tabindex="4" name="name"> </div> 

I am using tabindex in this example because your question doesn't provide any insight as to why I can't use it. 我在此示例中使用的是tabindex ,因为您的问题并未提供任何关于为什么我不能使用它的见解。

You can create a variable and to keep track of current input. 您可以创建一个变量并跟踪当前输入。 And each time increase it. 并且每次增加它。 And focus the next input from array of inputs.Below is the code 然后集中输入数组中的下一个输入。下面是代码

 let inputs = document.querySelectorAll('input'); let i = 0; inputs.forEach(inp => inp.oninput = () => inputs[i+1] && inputs[++i].focus()) 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div> <input type="number" name="name"> <span></span> <input type="number" name="name"> <span></span> <input type="number" name="name"> <span></span> <span></span> <input type="number" name="name"> </div> 

There is a rather simple solution. 有一个相当简单的解决方案。 Use id attribute on the input elements. 在输入元素上使用id属性。 Then you can automatically move to the next element with a simple switch within your validation function. 然后,您可以通过验证功能中的简单开关自动移至下一个元素。

Such as: 如:

function validateInput(input){
  //Your validation code
  switch(input.id){
    case'input1':
      document.getElementById('input2').focus();
      break;
    case'input2':
      document.getElementById('input3').focus();
      break;
    case'input3':
      document.getElementById('input4').focus();
      break;
  }
}

<input type='number' name='name' id='input1' onchange='validateInput(this)'>
<input type='number' name='name' id='input2' onchange='validateInput(this)'>

Then you don't have to worry about where in the document it is, you can always find it. 然后,您不必担心它在文档中的位置,可以随时找到它。 You could even do your validation from within the switch or send it to an external function and continue within validateInput with a non-null return 您甚至可以从开关内部进行验证,或将其发送到外部函数,然后在validateInput中继续,返回非null

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM