Jquery 在 Enter 键上选择 NEXT 文本字段

2022-01-13 00:00:00 keyboard jquery javascript html

我使用 jquery 创建了一个页面,加载时它会自动选择第一个文本字段.我希望它在按下 ENTER 键时移动到下一个字段.

I have made a page using jquery, and on load it selects the first text field automatically. I want it to then move to the next field when the ENTER key is pressed.

$('.barcodeField input').bind('keyup', function(event) {
    if(event.keyCode==13){
       $("this + input").focus();     
    }
});

我在网上找不到任何有效的东西.我已经搜索了论坛.

I can't find anything that works on the net. And I've scoured the forums.

推荐答案

我创建了一个小函数,可以满足你的需求.这是我使用的版本,因此您可能需要更改类名,但您应该明白了.

I've created a little function which can do what you need. This is the version I use so you may need to change the class names but you should get the idea.

<script type="text/javascript">
$(document).ready(function(){
$(".vertical").keypress(function(event) {
        if(event.keyCode == 13) { 
        textboxes = $("input.vertical");
        debugger;
        currentBoxNumber = textboxes.index(this);
        if (textboxes[currentBoxNumber + 1] != null) {
            nextBox = textboxes[currentBoxNumber + 1]
            nextBox.focus();
            nextBox.select();
            event.preventDefault();
            return false 
            }
        }
    });
})
</script>

所以基本上:-

  • 获取所有匹配 .vertical 的输入字段
  • 查找当前文本框
  • 寻找下一个
  • 将焦点放在那个上

相关文章