javascript - 每次按下 TAB 键选择不同的单词

标签 javascript jquery keyboard-shortcuts

在句子中的每个单词上移动。

我在我的应用程序中创建了用于输入的快捷键,因为它会移动并聚焦我页面中的每个输入控件。

我需要为选项卡设置键盘快捷键,因为它必须选择某些文本框中的句子的每个字符串。例如 txtAddress 包含像“嗨,我是新用户”这样的值,如果我按 tab 键,它必须选择一个字符串“hi”,然后是“i”,然后是“am”,然后是“new”,然后是“user”,之后它必须聚焦下一个输入控件。

我尝试使用以下 JS 来聚焦下一个输入控件,但不知道如何选择文本框中的每个单词。

$(document).unbind('keydown');
$(document).bind('keydown', 'tab', function assets() {
    try {
        var inputs = $(":input:not(input[type='hidden'])");
        var CurInput = inputs.get(inputs.index(document.activeElement));
        var nextInput = inputs.get(inputs.index(document.activeElement) + 1);
        if (CurInput && nextInput.type == "text" && CurInput.style.display != "none") {
            var strval = CurInput.value;
            if (!strval) {
                if (nextInput && nextInput.type != "hidden" && nextInput.style.display != "none") {
                    nextInput.focus();
                }
            }
        }
        else if (nextInput && nextInput.type != "hidden" && nextInput.style.display != "none") {
            nextInput.focus();
        }
        return false;
    }
    catch (e) {
    }
});

最佳答案

http://jsbin.com/cihahigevo/1/edit?html,js,output

var textarea = $('textarea')[0],
    index = 0;

$(document).on('keydown.tab', function(e){
  if( e.keyCode == 9 ){
    textarea.focus();
    textarea.value = textarea.value.trim() + ' ';
    index = textarea.value.indexOf(' ', index) + 1;
    textarea.setSelectionRange(0, index);
  }
  return false;
});

关于javascript - 每次按下 TAB 键选择不同的单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27146421/

相关文章:

jquery - 使用 jQuery 动态创建元素

javascript - jQuery :visible working but :hidden does not

visual-studio-code - 如何使用键盘快捷键更改 vscode 选项卡顺序

javascript - 浏览器/HTML 如何决定光标焦点应该在哪里?

javascript - 如何使用 jQuery flot 添加水平条?

javascript - 如何用JS模拟按键?

visual-studio-code - VSCode 按键绑定(bind)隐藏资源管理器

xcode - "Key Equivalent"输入类似Xcode?

javascript - 将 JavaScript 表存储在变量中

javascript - jQuery 数据表导出按钮动态文件名和标题