我想检测用户是否使用jQuery按下了Enter键。

这怎么可能?它需要插件吗?

看起来我需要使用keypress()方法。

该命令是否存在浏览器问题?比如,是否存在我应该知道的浏览器兼容性问题?


当前回答

我使用了$(document).on(“keydown”)。

某些浏览器不支持keyCode。与which相同,因此如果不支持keyCode,则需要使用which,反之亦然。

$(document).on(“keydown”,函数(e){常量ENTER_KEY_CODE=13;const ENTER_KEY=“回车”;var代码=e.keyCode | | e.whichvar键=e.key如果(代码==ENTER_KEY_code||键==ENTER_BEY){console.log(“按下Enter键”)}});<script src=“https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js“></script>

其他回答

我无法使Paolo Bergan蒂诺发布的代码正常工作,但当我将其更改为$(document)和e.which而不是e.keyCode时,我发现它工作得很完美。

$(document).keypress(function(e) {
    if(e.which == 13) {
        alert('You pressed Enter!');
    }
});

链接到JS Bin上的示例

Andrea答案的一个小扩展使helper方法在您可能还想捕获修改的回车(即Ctrl+enter或Shift+enter)时更有用。例如,此变体允许如下绑定:

$('textarea').enterKey(function() {$(this).closest('form').submit(); }, 'ctrl')

当用户按下Ctrl+Enter并将焦点放在表单的文本区域时提交表单。

$.fn.enterKey = function (fnc, mod) {
    return this.each(function () {
        $(this).keypress(function (ev) {
            var keycode = (ev.keyCode ? ev.keyCode : ev.which);
            if ((keycode == '13' || keycode == '10') && (!mod || ev[mod + 'Key'])) {
                fnc.call(this, ev);
            }
        })
    })
}

(另请参见*Ctrl+Enter在TEXTAREA中使用jQuery)

我编写了一个小插件,以便更容易地绑定“按下回车键”事件:

$.fn.enterKey = function (fnc) {
    return this.each(function () {
        $(this).keypress(function (ev) {
            var keycode = (ev.keyCode ? ev.keyCode : ev.which);
            if (keycode == '13') {
                fnc.call(this, ev);
            }
        })
    })
}

用法:

$("#input").enterKey(function () {
    alert('Enter!');
})

检测用户是否按下Enter键的简单方法是使用键号。Enter键编号等于13。

要检查设备中密钥的值,请执行以下操作:

$("input").keypress(function (e) {
  if (e.which == 32 || (65 <= e.which && e.which <= 65 + 25)
                    || (97 <= e.which && e.which <= 97 + 25)) {
    var c = String.fromCharCode(e.which);
    $("p").append($("<span/>"))
          .children(":last")
          .append(document.createTextNode(c));
  } else if (e.which == 8) {
    // Backspace in Internet Explorer only is on keydown
    $("p").children(":last").remove();
  }
  $("div").text(e.which);
});

按Enter键,结果为13。使用键值,您可以调用函数或执行任意操作:

    $(document).keypress(function(e) {
      if(e.which == 13) {
        console.log("The user pressed the Enter key");

        // The code you want to run
      }
    });

如果您想在按下Enter键后锁定按钮,可以使用以下代码:

$(document).bind('keypress', function(e) {
  if(e.which === 13) { // Return
     $('#buttonname').trigger('click');
  }
});

这就是我解决问题的方法。你应该使用return false;

$(document).on('按键',函数(e){如果(e.whit==13){$('#sub_btn').触发器('click');alert('您在某处按下了“Enter”键');return false;}});<script src=“https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js“></script><form action=“”method=“post”id=“sub_email_form”><div class=“modal header”><button type=“button”class=“close”id=“close”数据消除=“mode”>&times</按钮><h4 class=“modal title”>订阅我们的技术分析</h4></div><div class=“modal body”><p>注册我们的定期技术分析更新,以查看直接在收件箱中提供的建议</p><div class=“input group”><input type=“email”name=“sub_email”id=“sub_email”class=“form control”placeholder=“Enter your email”required></div><span id=“save error”></span></div><div class=“modal footer”><div class=“input group append”><input type=“submit”class=“btn btn primary sub_btn”id=“sub_btn”name=“sub_btn”value=“Subscribe”></div></div></form>