Html 自动将光标从一个文本框移动到另一个文本框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17522431/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 10:52:29  来源:igfitidea点击:

Automatically Move Cursor from One Textbox to Another

javascripthtml

提问by Rajesh

I am making a Web application that validates passkeys and displays some values there are four passkeys for a file to be entered Validate it, I am entering the passkeys just like we enter the Credit Card Number in the Payment gateway. In my present application I have enter one Passkey then have to press Tab or using the Mouse I have to select the Next Textbox to enter next Passkey, How do I Make the mouse Cursor to Jump automatically from one Textbox to Another Textbox after its maximum value filled like in Payment gateways

我正在制作一个验证密码并显示一些值的 Web 应用程序,有四个密码用于输入文件 验证它,我输入密码就像我们在支付网关中输入信用卡号码一样。在我目前的应用程序中,我输入了一个密码,然后必须按 Tab 或使用鼠标我必须选择下一个文本框才能输入下一个密码,如何使鼠标光标在其最大值后自动从一个文本框跳转到另一个文本框像支付网关一样填充

回答by rajeemcariazo

You can do pure javascript like this:

您可以像这样执行纯 javascript:

<script type="text/javascript">
function ValidatePassKey(tb) {
  if (tb.TextLength >= 4)
    document.getElementById(tb.id + 1).focus();
  }
}
</script>

<input id="1" type="text" onchange="ValidatePassKey(this)" maxlength="4">
<input id="2" type="text" onchange="ValidatePassKey(this)" maxlength="4">
<input id="3" type="text" onchange="ValidatePassKey(this)" maxlength="4">
<input id="4" type="text" maxlength="4">

回答by CodeCaster

In my present application I have enter one Passkey then have to press Tab or using the MouseI have to select the Next Textbox to enter next Passkey

在我目前的应用程序中,我输入了一个密码,然后必须按 Tab 或使用鼠标我必须选择下一个文本框以输入下一个密码

Don't do that. Just use the Control.Focus()method.

不要那样做。就用这个Control.Focus()方法吧。

When in HTML, you can use jQuery's focus():

在 HTML 中,您可以使用 jQuery 的focus()

$("#textbox").focus();

回答by sohaiby

You can use JavaScript's Onchange Event (or JQuery may be) on the Textbox which calls a method, where You can check the value of the Textbox if it equals the Maximum value , then setFocus on the next Textbox.

您可以在调用方法的 Textbox 上使用 JavaScript 的 Onchange 事件(或 JQuery),您可以在其中检查 Textbox 的值是否等于 Maximum value ,然后在下一个 Textbox 上设置焦点。

回答by Pharap

Use the TextBox.Focus() method on the next TextBox. Use the first TextBox's TextBox.TextChanged event to test if focus should be changed and to call the Focus method on the next TextBox.

在下一个 TextBox 上使用 TextBox.Focus() 方法。使用第一个 TextBox 的 TextBox.TextChanged 事件来测试是否应该更改焦点并在下一个 TextBox 上调用 Focus 方法。

回答by sibaspage

you can create a directive also to get the position move

您也可以创建一个指令来获取位置移动

<a href="https://plnkr.co/edit/G6sFMM9vaR6nQfVaMI2E?p=preview">directive 1</a>

<a href="https://plnkr.co/edit/G32KUITspNp1qsq6gleI?p=preview">directive 2</a>