javascript - Enter 键跟随 tabindex(在 Enter 键更改为 Tab 的情况下)

标签 javascript jquery

我的表单中有一个 3x3 的表格。默认情况下,Tab 键在这些输入字段上沿水平方向移动光标。当给出 tabindex 时(如下例所示),tab 键按照我的需要按列而不是按行移动光标。

通过SO答案的各种来源,我想出了Jquery来使用回车键作为选项卡。但是,无法弄清楚如何按照上面实现的 tabindex 进行操作,即通过按 Enter 键而不是光标按行移动,我希望它按列移动。以下是我迄今为止所掌握的内容,感谢您的帮助。

演示其当前的工作原理。 http://jsfiddle.net/unCu5/125/

以下代码来源:jquery how to catch enter key and change event to tab

<table>
    <tr>
        <td><input tabindex="1" placeholder="1" /></td>
        <td><input tabindex="2" placeholder="2" /></td>
        <td><input tabindex="3" placeholder="3" /></td>
    </tr><tr>
        <td><input tabindex="1" placeholder="1" /></td>
        <td><input tabindex="2" placeholder="2" /></td>
        <td><input tabindex="3" placeholder="3" /></td>
    </tr><tr>
        <td><input tabindex="1" placeholder="1" /></td>
        <td><input tabindex="2" placeholder="2" /></td>
        <td><input tabindex="3" placeholder="3" /></td>
    </tr>
</table>

$('input').live("keypress", function(e) {
  /* ENTER PRESSED*/
  if (e.keyCode == 13) {
    /* FOCUS ELEMENT */
    var inputs = $(this).parents("form").eq(0).find(":input");
    var idx = inputs.index(this);

    if (idx == inputs.length - 1) {
      inputs[0].select()
    } else {
      inputs[idx + 1].focus(); //  handles submit buttons
      inputs[idx + 1].select();
    }
    return false;
  }
})

@Dekel 解决方案适用于他显示的 html 场景,但我在查看源代码时有不同类型的 HTML。我该如何解决这个问题

最佳答案

您可以找到下一个元素(基于 tabindex)并关注他,而不是仅仅关注下一个 input 元素:

$('input[tabindex^="2"]');

检查这个例子:

$(document).ready(function () { // Will only run once the page Document Object Model (DOM) is ready for JavaScript code 
    // Create a jQuery object containing the html element 'input'
    // Create a .not() method to exclude buttons for keypress event
    $(":input:not(:disabled)").not($(":button")).keypress(function(evt) {
        // If the keypress event code is 13 (Enter)
        if (evt.keyCode == 13) {
            // get the attribute type and if the type is not submit
            itype = $(this).prop('type');
            if (itype !== 'submit') {
                currentTabindex = $(this).attr('tabindex');
                if (currentTabindex) {
                    nextInput = $('input[tabindex^="'+ (parseInt(currentTabindex)+1) +'"]');
                    if (nextInput.length) {
                        nextInput.focus();
                        return false;
                    }
                }
            }
        }
    });
});
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<table>
    <tr>
        <td><input tabindex="1" placeholder="1" /></td>
        <td><input tabindex="4" placeholder="4" /></td>
        <td><input tabindex="7" placeholder="7" /></td>
    </tr><tr>
        <td><input tabindex="2" placeholder="2" /></td>
        <td><input tabindex="5" placeholder="5" /></td>
        <td><input tabindex="8" placeholder="8" /></td>
    </tr><tr>
        <td><input tabindex="3" placeholder="3" /></td>
        <td><input tabindex="6" placeholder="6" /></td>
        <td><input tabindex="9" placeholder="9" /></td>
    </tr>
</table>

The code doesn't support go back from the last input to the first. You will need to write it explicitly.


更新版本 - 修复错误的 tabindex

最初的问题没有提到 tabindex 可以重复或没有顺序值。 此代码将根据代码中的顺序以及 tabindex 的值来“修复”tabindex 的值。它将支持重复的 tabindex 值和非顺序值(1、2、3、6、7):

function fixTabIndex() {
    // Find all inputs. Their order will be the same order they appear inside your html.
    inputs = $('input');
    // Save the original HTML order of the inputs (we need this to compare their order in the HTML in case or equal two tabindex 
    inputs_original = $('input');
    
    // Sort the inputs by their tabindex values and their position in the DOM
    // More info on Array.prototype.sort: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort
    inputs.sort(function(a, b) {
        if ($(a).attr('tabindex') == $(b).attr('tabindex')) {
            // If tabindex is equal - sort by the position in the DOM
            if (inputs_original.index(a) < inputs_original.index(b)) {
                return -1;
            } else {
                return 1;
            }
        } else if ($(a).attr('tabindex') < $(b).attr('tabindex')) {
            return -1;
        } else {
            return 1;
        }
    });
    // Set the new value of the tabindex based on the position in the sorted array
    inputs.each(function(i, el) {
        $(el).attr('tabindex', i+1);
    });
}

$(document).ready(function () {
    // First we need to fix the tabindex values:
    fixTabIndex();
    $("input").keypress(function(evt) {
        // If the keypress event code is 13 (Enter)
        if (evt.keyCode == 13) {
            // Make sure this is not a submit input
            if ($(this).prop('type') !== 'submit') {
                currentTabindex = $(this).attr('tabindex');
                if (currentTabindex) {
                    nextInput = $('input[tabindex^="'+ (parseInt(currentTabindex)+1) +'"]');
                    if (nextInput.length) {
                        nextInput.focus();
                        return false;
                    }
                }
            }
        }
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<table>
    <tr>
        <td><input tabindex="1" placeholder="1" /></td>
        <td><input tabindex="2" placeholder="2" /></td>
        <td><input tabindex="3" placeholder="3" /></td>
    </tr><tr>
        <td><input tabindex="1" placeholder="1" /></td>
        <td><input tabindex="2" placeholder="2" /></td>
        <td><input tabindex="3" placeholder="3" /></td>
    </tr><tr>
        <td><input tabindex="1" placeholder="1" /></td>
        <td><input tabindex="2" placeholder="2" /></td>
        <td><input tabindex="3" placeholder="3" /></td>
    </tr>
</table>

关于javascript - Enter 键跟随 tabindex(在 Enter 键更改为 Tab 的情况下),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38489379/

相关文章:

javascript - 我如何获取 Amazon Fire 电视连续剧......?

javascript - 我使用 JQuery 将新的 <li> 项添加到现有的 <ul> 但新的 <li> 与其他 <li> 不同

javascript - jQuery - 悬停时淡入淡出动画

javascript - 使用 Javascript 渲染 DB 值

javascript - 如何制作类似Github的面包屑和shifting view效果

javascript - Auth0 实现 - 获取访问 token

c# - 单击按钮激活/停用 javascript

javascript - JQuery Mobile - 将文本区域附加到现有页面

javascript - 用 base64 编码字符串替换 css 文件中的所有图像路径 - 汇总

javascript - AngularJS - 为什么需要模块依赖?