【问题标题】:How to simulate TAB on ENTER keypress in javascript or jQuery如何在 javascript 或 jQuery 中模拟 ENTER 按键上的 TAB
【发布时间】:2012-01-22 16:38:13
【问题描述】:

我想在页面中的所有输入中更改 keydown ( key press ) 中的键码。我想用 TAB 键码替换 Enter 键码。我该怎么做?

谢谢


编辑 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 键时转到下一个控件。

谢谢

【问题讨论】:

  • 你可以在stackoverflow.com/questions/1009808/…找到类似的问题
  • 我相信值得注意的是,像这样更改浏览器的默认行为通常不是一个好主意。这与用户在按下按键时所期望的结果背道而驰。
  • @ChristoferEliasson:当然,这取决于您的目标用户。如果您可以教育您的用户(例如,如果他们在大厅里),这可能是一个非常好的工作流程改进。

标签: javascript jquery


【解决方案1】:

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

PlusAsTab:一个 jQuery 插件,使用数字键盘加键作为 Tab 键等效项。

由于您想要 enter/↵ 代替,您可以设置选项。找出您要与jQuery event.which demo 一起使用的密钥。

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 覆盖 - 请参阅 autonavigation of radio buttons in that demo。

<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 as tab demo 中亲自试用。

【讨论】:

  • 我喜欢你在 Joel 中的插件,但在使用 enter 作为选项卡示例时,它似乎没有注意 tabindex。 :(
  • @EdDeGagne:tabindex 被设计忽略 - 请参阅 SkipOnTab versus tabindex。可能应该写一些类似的东西或从 PlusAsTab 和 EmulateTab 链接到该页面。
【解决方案2】:

这段代码是用制表符替换回车:

$("#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);
   }
});

Live Demo

更新:

这段代码专注于下一个元素:

$(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();
                }
            }
        }
    });
});

【讨论】:

  • 我想,我误解了这个问题。您的意思是专注于 TAB 新闻上的其他元素?
  • 它不起作用。请参阅我的编辑 1。我希望焦点转到下一个控件
  • 谢谢,但在单选按钮中,它会导致移动到具有单选类型的每个输入中。请参阅:jsfiddle.net/xjHvK
  • 它对我有用,只是它没有在按钮上的 Enter 上提交表单
【解决方案3】:

希望这可行

$('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();
  }
});

【讨论】:

  • @Web Developer 第二个选项只有在输入下一个元素而不是标签等其他任何东西时才有效...
  • 第一个解决方案(更改 keyCode)不再起作用。 :-(
【解决方案4】:
$('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/

【讨论】:

    【解决方案5】:

    我这样做的方法是在你的选择上使用 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;
                }
    
            });
        }
    });
    

    【讨论】:

      【解决方案6】:

      我认为这项工作:

       $('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;
              }
          });
      

      【讨论】:

        【解决方案7】:

        我创建了一个simple jQuery plugin,它确实解决了这个问题。它使用 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;
            }
        });
        

        【讨论】:

          【解决方案8】:
          $(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();
                  }
              });
          });
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2012-03-02
            • 1970-01-01
            • 2010-10-16
            • 2015-10-19
            • 1970-01-01
            • 1970-01-01
            • 2011-08-24
            • 1970-01-01
            相关资源
            最近更新 更多