如何在javascript或jQuery中模拟ENTER键上的TAB

How to simulate TAB on ENTER keypress in javascript or jQuery

本文关键字:ENTER TAB 模拟 javascript jQuery      更新时间:2023-09-26

我想在页面的所有输入中更改键下键(按键(中的键码。我想将输入键代码替换为 TAB 键代码。我该怎么做?

谢谢


编辑 1(

请考虑以下代码:

<div>
    <asp:RadioButtonList ID="RadioButtonList1" runat="server">
        <asp:ListItem>1</asp:ListItem>
        <asp:ListItem>2</asp:ListItem>
        <asp:ListItem>3</asp:ListItem>
        <asp:ListItem>4</asp:ListItem>
    </asp:RadioButtonList>
    <br />
    <br />
    <asp:TextBox ID="TextBox1" runat="server">3333</asp:TextBox>
    <br />
    <br />
    <asp:DropDownList ID="DropDownList1" runat="server">
        <asp:ListItem>1</asp:ListItem>
        <asp:ListItem>2</asp:ListItem>
        <asp:ListItem>3</asp:ListItem>
    </asp:DropDownList>
</div>

我希望当用户在上述控件焦点上按 Enter 键时转到下一个控件。

谢谢

我遇到了类似的问题,我想按数字键盘上的 + 键转到下一个字段。现在我发布了一个库,我认为它会对你有所帮助。

PlusAsTab:一个jQuery插件,使用numpad plus键作为Tab键等效项。

由于您想要输入/,因此可以设置选项。找出要与 jQuery 事件一起使用的键。

JoelPurra.PlusAsTab.setOptions({
  // Use enter instead of plus
  // Number 13 found through demo at
  // https://api.jquery.com/event.which/
  key: 13
});

然后,通过将plus-as-tab="true"添加到要在其中使用的表单字段 enter-as-tab 或包含这些表单字段的其他元素来启用该功能。单选按钮应该不是问题,因为它们被我的另一个库 EmulateTab 所涵盖 - 请参阅该演示中单选按钮的自动导航。

<div plus-as-tab="true">
  <!-- all focusable elements inside the <div> will be enabled -->
  <asp:RadioButtonList ID="RadioButtonList1" runat="server">
    <!-- Radio buttons should not be a problem. -->
  </asp:RadioButtonList>
</div>

您可以在PlusAsTab中亲自尝试,输入为选项卡演示。

此代码将 enter 替换为制表符:

$("#wmd-input").bind("keypress", function(e) {
   if (e.keyCode == 13) {
      var input = $(this);
      var inputVal = input.val();
      setTimeout(function() {
        input.val(inputVal.substring(0,inputVal.length) + "'t");
      }, 1);
   }
});

现场演示

更新:

这段代码将重点放在下一个元素上:

$(document).ready(function () {
    $("input,select").bind("keydown", function (e) {
        if (e.keyCode == 13) {
            var allInputs = $("input,select");
            for (var i = 0; i < allInputs.length; i++) {
                if (allInputs[i] == this) {
                    while ((allInputs[i]).name == (allInputs[i + 1]).name) {
                        i++;
                    }
                    if ((i + 1) < allInputs.length) $(allInputs[i + 1]).focus();
                }
            }
        }
    });
});

希望这有效

$('input,textarea').keydown(function(){
  if(event.keyCode==13) {
    event.keyCode = 9;
  }
});

编辑

试试这个 http://jsfiddle.net/GUmUg/。使用选择器来使其工作,因为我不知道 asp

$('input,textarea').keypress(function(e){
    if(e.keyCode==13) {
   $(this).next().focus();
  }
});
$('input').on('keydown',function(e){
    var keyCode = e.keyCode || e.which;
     if(e.keyCode === 13) {
      e.preventDefault();
      $('input')[$('input').index(this)+1].focus();
      }
});

在这里检查小提琴:http://jsfiddle.net/Pd5QC/

我这样做的方法是使用 jquery 来选择每个元素,并在您打开当前之后专注于元素。

$(document).on('keyup', '.my-input', function (ev) {
    if (ev.keyCode == '13') {
        var currentInput = this;
        var isOnCurrent = false;
        $('.my-input').each(function () {
            if (isOnCurrent == true) {
                $(this).focus();
                return false;
            }
            if (this == currentInput) {
                isOnCurrent = true;
            }
        });
    }
});

我认为这项工作:

 $('input').live("keypress", function (e) {
        /* ENTER PRESSED*/
        var OffSet = 0;
        if (e.keyCode == 13) {
            /* FOCUS ELEMENT */
            if ($(this).is("input[type='radio']")) {
                var tblID = $(this).closest('table').attr('id');
                var radios = $('#' + tblID).find(":input");
                //alert(radios.index(this));
                OffSet = radios.length - radios.index(this) - 1;
            }
            //alert(OffSet);
            var inputs = $(this).parents("form").eq(0).find(":input");
            var idx = inputs.index(this);
            inputs[idx + OffSet].blur();
            try {
                inputs[idx + OffSet].selectionStart = inputs[idx + OffSet].selectionEnd = -1;
            } catch (e) {
            }
            if (idx == inputs.length - 1) {
                inputs[0].select();
            } else {
                inputs[idx + 1 + OffSet].focus(); //  handles submit buttons
                try {
                    inputs[idx + 1 + OffSet].select();
                } catch (e) {
                }
            }
            return false;
        }
    });

我创建了一个简单的jQuery插件,它确实解决了这个问题。它使用 jQuery UI 的 ':tabbable' 选择器来查找下一个 'tabbable' 元素并选择它。

用法示例:

// Simulate tab key when enter is pressed           
$('.myElement').bind('keypress', function(event){
    if(event.which === 13){
        if(event.shiftKey){
            $.tabPrev();
        }
        else{
            $.tabNext();
        }
        return false;
    }
});
$(document).ready(function() {
//Objetos con CssClass="EntTab" sustituye el Enter (keycode 13) por un Tabulador (keycode 9)!!
    $(".EntTab").bind("keypress", function(e) {
        if (e.keyCode == 13) {
            var inps = $("input, select"); //add select too
            for (var x = 0; x < inps.length; x++) {
                if (inps[x] == this) {
                    while ((inps[x]).name == (inps[x + 1]).name) {
                        x++;
                    }
                    if ((x + 1) < inps.length) $(inps[x + 1]).focus();
                }
            } e.preventDefault();
        }
    });
});