简体   繁体   English

如何将下一个输入字段聚焦在按键上

[英]How to focus next input field on keypress

I am making a mobile application.我正在制作一个移动应用程序。 I want to implement this function where on pressing the key, the next input field is focused.我想实现这个功能,在按下键时,下一个输入字段被聚焦。 I tried many things but none seem to be working.我尝试了很多东西,但似乎都没有奏效。 This is my code.这是我的代码。 I tried using onClick but when i touch it, it goes to the next field.我尝试使用 onClick 但当我触摸它时,它会转到下一个字段。

<input type="number" class="mobile-verify pass" maxlength="1" name="code[]" />
<input type="number" class="mobile-verify pass" maxlength="1" name="code[]" />
<input type="number" class="mobile-verify pass" maxlength="1" name="code[]" />
<input type="number" class="mobile-verify pass" maxlength="1" name="code[]" />

I want a pure Jquery or Javascript solution.我想要一个纯JqueryJavascript解决方案。 Thanks in advance.提前致谢。

How about:怎么样:

$('input.mobile-verify.pass').on('keyup', function() {
    if ($(this).val()) {
        $(this).next().focus();
    }
});

So on key up, if there is a value, focus the next.所以在key up,如果有值,关注下一个。 Using keyup allows you to validate the contents rather than skipping right away.使用 keyup 可以让您验证内容而不是立即跳过。 They might switch to number mode on iOS for example which would trigger the focus if you simply use keypress.例如,他们可能会在 iOS 上切换到数字模式,如果您只是使用按键,则会触发焦点。

Codepen: http://codepen.io/anon/pen/qaGKzk代码笔: http ://codepen.io/anon/pen/qaGKzk

Use .next() and .trigger() :使用.next().trigger()

 $(".mobile-verify.pass").on("keypress", function(e){ $(this).next().trigger("focus"); })
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="number" class="mobile-verify pass" name="code[]" /> <input type="number" class="mobile-verify pass" name="code[]" /> <input type="number" class="mobile-verify pass" name="code[]" /> <input type="number" class="mobile-verify pass" name="code[]" />

Side note: Find another solution for the maxlength attribute functionality, as per maxlength ignored for input type=“number”旁注:为maxlength属性功能找到另一个解决方案,根据输入 type="number" 忽略的 maxlength

JavaScript and events keypress and focus and also key numbers validation: JavaScript 和事件keypressfocus以及关键数字验证:

 document .querySelectorAll('.mobile-verify.pass') .forEach(el => el.onkeyup = e => { if (e.target.value) { el.nextElementSibling.focus() } } )
 <input type="number" class="mobile-verify pass" maxlength="1" name="code[]" /> <input type="number" class="mobile-verify pass" maxlength="1" name="code[]" /> <input type="number" class="mobile-verify pass" maxlength="1" name="code[]" /> <input type="number" class="mobile-verify pass" maxlength="1" name="code[]" />

在 jQuery 中尝试.keypress().focus()函数。

Use .使用 . keypress() and .focus()按键().focus()

The snippet :片段:

 $(document).ready(function (){ $("input.mobile-verify.pass").keypress(function(){ $(this).next().trigger('focus'); //or $(this).next().focus(); }); });
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="number" class="mobile-verify pass" maxlength="1" name="code[]" /> <input type="number" class="mobile-verify pass" maxlength="1" name="code[]" /> <input type="number" class="mobile-verify pass" maxlength="1" name="code[]" /> <input type="number" class="mobile-verify pass" maxlength="1" name="code[]" />

With full JS完整的JS

 var allElements = document.querySelectorAll('.mobile-verify.pass'); var i; for (i = 0; i < allElements.length; i++) { var el = allElements[i]; el.addEventListener("keypress", function () { this.nextSibling.nextSibling.focus(); }); }
 <input type="number" class="mobile-verify pass" maxlength="1" name="code[]" /> <input type="number" class="mobile-verify pass" maxlength="1" name="code[]" /> <input type="number" class="mobile-verify pass" maxlength="1" name="code[]" /> <input type="number" class="mobile-verify pass" maxlength="1" name="code[]" />

NB : if you ask, why 2 nextSibling, the answer is that the first sibling is the text representing the small space between 2 elements注意:如果你问,为什么 2 nextSibling,答案是第一个兄弟是代表 2 个元素之间的小空间的文本

Others have already written the solutions.其他人已经编写了解决方案。 I just wanted to add that the part我只是想补充一点

[].slice.call

is what solved my problem.是什么解决了我的问题。 I couldn't focus an element because it was of type Element, and not HtmlElement.我无法聚焦一个元素,因为它是 Element 类型,而不是 HtmlElement。 But somehow that part of code is converting my querySelectorAll list of Elements to HtmlElements.但不知何故,这部分代码正在将我的 querySelectorAll 元素列表转换为 HtmlElements。

Thank you Yosvel Quintero谢谢约斯维尔·金特罗

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

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