在说检测回车符之前,需要了解keydown和keypress的区别

 

比如你可以将检测事件绑定在input上,如下所示:

<input name="remark" id="remark">
$("#remark").keydown(function () {
    var theEvent = window.event || event;
    var code = theEvent.keyCode || theEvent.which;
    if (code == 13) {
        console.log($("#remark").val());
    }
});

如果想在整个页面范围内绑定按钮事件,就可以把上面代码中的$("#remark")改成$("body"),这样就可以在整个页面范围内检测按钮事件。

 

=======================================

 

如果想用传统的方式来绑定,也可以这样:

<input name="remark" id="remark" onkeydown="ok(event)">
function ok(event) {
    var theEvent = window.event || event;
    var code = theEvent.keyCode || theEvent.which;
    if (code == 13) {
        console.log($("#remark").val());
    }
});

 

相关文章:

  • 2022-12-23
  • 2021-11-16
  • 2021-12-04
  • 2021-05-22
  • 2021-09-16
  • 2021-08-31
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-11-17
  • 2021-09-09
  • 2022-12-23
  • 2021-11-18
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案