使用jQuery的Tab行为 [英] Tab behavior using jQuery

查看:119
本文介绍了使用jQuery的Tab行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些文本框在我的形式。我不知道他们的 id 值。我想通过按Enter键将光标位置从现有的位置更改为下一个字段。

请注意,光标可能在第一时间处于不同的位置。

I have a some text boxes in my form. I don't know their id values. I want change the cursor position from the existing one to the next field by pressing Enter.
Pay attention that the cursor may be in different positions at the first time.

您的建议是什么?

推荐答案

你的标记,所以我不能假设你的结构(例如使用 .next()如果他们是兄弟元素)。因此,假设您有输入文本,请尝试此示例(对于jQuery 1.7 +)

you didn't provide your markup so I can't make assumption about your structure (e.g. using .next() if they are sibling elements). So, supposing you have input text, try this example (for jQuery 1.7+)

$(document).ready(function() {

    var inputs = $('input[type="text"]');
    var len = inputs.length;

    inputs.on('keyup', function(evt) {
        if (evt.keyCode === 13) {
           var currentInput = inputs.index($(this));
           inputs.eq((currentInput < len - 1)
                     ? currentInput + 1
                     : 0).focus();

           evt.preventDefault();
        }
    });
});

当您按最后一个输入上的Enter键时,第一个输入将被聚焦(循环)。
查看jsfiddle示例: http://jsfiddle.net/8ruDV/

When you press enter on last input, the first input will be focused (in a loop). See a jsfiddle example: http://jsfiddle.net/8ruDV/

这篇关于使用jQuery的Tab行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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