首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用箭头键在文本输入字段中导航并返回

使用箭头键在文本输入字段中导航并返回
EN

Stack Overflow用户
提问于 2011-06-19 08:49:26
回答 2查看 8.1K关注 0票数 0

我正在尝试使用jQuery在多个输入字段之间构建一种简单的导航机制。代码的第一部分,通过使用向下箭头或return键跳过向下可以很好地工作,但当我通过查找向上箭头然后颠倒顺序添加第二个块以向后移动时,在第一个文本字段中键入的内容会立即跳到第二个文本字段。有什么想法吗?

代码语言:javascript
复制
<script type="text/javascript">
$(document).ready(function(){
    // get only input tags with class data-entry
    textboxes = $("input.data-entry");
    // now we check to see which browser is being used
    if ($.browser.mozilla) {
        $(textboxes).keypress (checkForAction);
    } else {
        $(textboxes).keydown (checkForAction);
    }
});

function checkForAction (event) {
    if (event.keyCode == 13 || 40) {
          currentBoxNumber = textboxes.index(this);
        if (textboxes[currentBoxNumber + 1] != null) {
            nextBox = textboxes[currentBoxNumber + 1]
            nextBox.focus();
            nextBox.select();
            event.preventDefault();
            return false;
        }
    }
    if (event.keyCode == 38) {
          currentBoxNumber = textboxes.index(this);
        if (textboxes[currentBoxNumber - 1] != null) {
            prevBox = textboxes[currentBoxNumber - 1]
            prevBox.focus();
            prevBox.select();
            event.preventDefault();
            return false;
        }
    }
}
</script>
EN

回答 2

Stack Overflow用户

发布于 2011-06-19 08:54:35

if (event.keyCode == 13 || 40) {...更改为if (event.keyCode == 13 || event.keyCode == 40) { ...

票数 1
EN

Stack Overflow用户

发布于 2012-03-14 17:42:03

这个博客会对你有所帮助。

演示页面

http://jqdev.blogspot.in/2012/01/traverse-cursor-through-text-box-with.html

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6399869

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档