如何防止光标/插入符号在输入文本元素内移动?
我正在尝试构建一个表单,其工作方式类似于 Google 搜索页面以及 Gmail 和其他几个页面(包括此页面 - 请参阅“提问”页面上的“标签”输入)。输入文本时,将出现一个选项列表,用户可以使用向上和向下箭头来选择他们想要的选项。
我遇到的问题是向上箭头将光标/插入符号放在字段的位置 0 处,向下箭头将其放在末尾。有没有一种简单的方法可以防止它移动到开头/结尾,或者我是否需要在函数运行之前找到插入符位置,然后将其重新定位到该位置?
这是在输入元素 (onkeyup) 中输入文本时运行的函数的开头。
var keycode = checkKeycode (event); // returns the keycode of the key pressed.
if (keycode == 38) { // up arrow
var selection = $(".optionsSelected").attr("id");
var selId = parseInt(selection.replace('briefOption', ''));
if (selId != 0) {
$(".optionsSelected").removeClass("optionsSelected");
var newSelId = selId - 1;
$("#briefOption"+newSelId).addClass("optionsSelected");
}
}
else if (keycode == 40) { // down arrow
var selection = $(".optionsSelected").attr("id");
var selId = parseInt(selection.replace('briefOption', ''));
if (selId != numAvEmps && numAvEmps != 0) {
$(".optionsSelected").removeClass("optionsSelected");
var newSelId = selId + 1;
$("#briefOption"+newSelId).addClass("optionsSelected");
}
}
else if (keycode == 27) { // Escape
$("#docDropDown").css("display","none");
}
else if (keycode == 9 || keycode == 13) { //tab or enter
/* Fill form */
}
让我知道更多代码是否有用。感谢您的帮助。
I'm trying to build a form that works in a manner similar to the Google Search page, as well as Gmail and several other pages (including the this page - see the "Tags" input on the Ask Question page). As text is input, a list of options will appear, and the user can use up and down arrows to select the option they want.
The problem I am having is that the up arrow puts the cursor/caret in position 0 of the field, and down puts it at the end. Is there an easy way of keeping it from moving to the beginning/end, or do I need to find the caret position before my function is run, and reposition it to that spot after?
This is the beginning of the function that runs when when text is entered in the input element (onkeyup).
var keycode = checkKeycode (event); // returns the keycode of the key pressed.
if (keycode == 38) { // up arrow
var selection = $(".optionsSelected").attr("id");
var selId = parseInt(selection.replace('briefOption', ''));
if (selId != 0) {
$(".optionsSelected").removeClass("optionsSelected");
var newSelId = selId - 1;
$("#briefOption"+newSelId).addClass("optionsSelected");
}
}
else if (keycode == 40) { // down arrow
var selection = $(".optionsSelected").attr("id");
var selId = parseInt(selection.replace('briefOption', ''));
if (selId != numAvEmps && numAvEmps != 0) {
$(".optionsSelected").removeClass("optionsSelected");
var newSelId = selId + 1;
$("#briefOption"+newSelId).addClass("optionsSelected");
}
}
else if (keycode == 27) { // Escape
$("#docDropDown").css("display","none");
}
else if (keycode == 9 || keycode == 13) { //tab or enter
/* Fill form */
}
Let me know if any more code would be useful. Thanks for the help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要先存储插入符号,然后在完成操作后将其放回去。
像这样的:
HTML 主体:
Javascript:
You'd want to store the caret before and then when you're done doing things, set it back.
Something like this:
HTML body:
Javascript:
只是阻止事件的默认操作。如果您使用的是事件处理程序属性而不是
addEventListener
/attachEvent
,则可以通过返回false
来完成此操作,这将是最简单的方法:Just prevent the default action of the event. You can do this by returning
false
if you're using an event handler property rather thanaddEventListener
/attachEvent
, which would be the easiest way: