jquery,javascript:更改 keyup 上的输入值 - 将光标位置保留在末尾,但是
嘿伙计们, 我面临着一个有点烦人的错误,就我而言,它并不是真正用户友好的。
用户可以使用向上和向下箭头键设置输入字段的值。 焦点始终设置在输入字段,因此用户可以在使用箭头键选择值后进行键入。
我希望光标位置始终保留在输入值的最后一个字符处,以便用户可以在通过箭头键选择值后立即键入。如果您按向下箭头键,效果就很好,因为在这种情况下,光标会自动跳转到输入框的末尾。
如果按向上键,则会发生完全相反的情况 - 光标跳到开头。 我可以使用我在 stackoverflow 上找到的“setCursorPosition”脚本来解决这个问题。
然而,尽管它正在工作,但用户无法真正“感觉和看到”它。如果我按向上箭头键,光标仍显示在输入值的开头。一旦我开始输入,光标就会跳到末尾并且工作正常。
在这里查看: http://jsfiddle.net/3UHVY/1/
所以我想知道你是否有一个解决方案,以便当我按下向上或向下箭头键时,光标可能始终保留在输入框的末尾,并且它对用户来说也是可见的和逻辑的。现在,用户可能认为他必须抓住鼠标并直接跳回输入框以将光标设置到末尾。
hey guys,
i'm facing a little annoying bug that's in my case not really userfriendly.
The user is able to set values of an inputfield with the Up and Down Arrow keys.
The focus is always set to the input field so the user is able to type after he has selected a value with the Arrow keys.
I want the Cursor Position to ALWAYS REMAIN at the last character of the input value so the user can type immediately after selecting a value via the arrow keys. That works just fine if you hit the down-arrow key because in that case the cursor automatically jumps to the end of the input-box.
Exactly the opposite happens if you press the up key - the cursor jumps to the beginning.
I was able to fix that with a "setCursorPosition" Script i found here on stackoverflow.
However, despite the fact that it's working the user cannot really "feel and see" it. If I press the Up-Arrow key the cursor is still shown at the beginning of the input-value. As soon as I start typing the cursor jumps to the end and it works fine.
See it here: http://jsfiddle.net/3UHVY/1/
So I wonder if you have a solution so the cursor may always remain at the end of the input box when i hit the up or down arrow key and it's also visible and logic for the user. Right now the user might think he has to grab the mouse and jump right back to the input box to set the cursor to the end.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以通过在向上键时返回 false 来抑制浏览器的默认向上键行为:
http://jsfiddle.net/3UHVY /5/
或者,如果这不能满足您的需求,您可以挂钩 keyUp 事件并将光标移动到输入的末尾。
You can suppress the browser's default up-key behavior by returning false on key up:
http://jsfiddle.net/3UHVY/5/
Alternatively, if that doesn't satisfy your needs, you can hook on the keyUp event and move the cursor to the end of the input.