使用箭头键浏览文本输入字段并返回 [英] Navigating through text input fields using arrow keys and return

查看:94
本文介绍了使用箭头键浏览文本输入字段并返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用jQuery在多个输入字段之间构建一个简单的导航机制。代码的第一部分,使用向下箭头或返回键跳过工作正常,但当我添加第二个块以通过查找向上箭头然后反转顺序向后移动时,键入第一个文本字段向右跳到第二个。有什么想法?

I'm trying to build a simple navigation mechanism between multiple input fields using jQuery. The first part of the code, skipping down by using the down arrow or return key work fine, but when I added the second block to go backwards by looking for the up arrow and then reversing the order, typing in the first text field jumps right away to the second. Any thoughts?

<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>


推荐答案

更改 if(事件。 keyCode == 13 || 40){... to if(event.keyCode == 13 || event.keyCode == 40){...

这篇关于使用箭头键浏览文本输入字段并返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆