如何使用 jQuery 验证将光标放置在输入字段的末尾?
我试图在验证后将光标放置在输入字段的末尾。这工作正常,除非您输入太多字母(超过输入框的默认长度),最后一个字母不会显示,因为输入的值在清理后被重写。
如何修改此代码以将光标置于输入字段的末尾?
在 html 中:
<input type="text" id="myInput">
在 jQuery 中:
$("#myInput").keyup(
function(){
this.value = this.value.replace(/[^a-z]/gi,''); // only letters
}
);
I am trying to place the cursor at the end of the input field after validation. This works fine, except if you type too many letters (more than the default length of the input box), the last letters do not show, because the value of the input is rewritten after the sanitization.
How can I modify this code to put the cursor at the end of the input field?
In the html:
<input type="text" id="myInput">
In the jQuery:
$("#myInput").keyup(
function(){
this.value = this.value.replace(/[^a-z]/gi,''); // only letters
}
);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不要在
keyup
事件中进行验证,而是尝试使用keydown
事件,因为事件触发时字符尚未添加到输入字段。你可以做这样的事情......
Instead of validating in the
keyup
event, try using thekeydown
event as the character is not yet added to the input field when the event is fired.You can do something like this...