How to detect pressing Enter on keyboard using jQuery?

后端 未结 18 2534
有刺的猬
有刺的猬 2020-11-22 11:07

I would like to detect whether the user has pressed Enter using jQuery.

How is this possible? Does it require a plugin?

EDIT: It looks like I need

18条回答
  •  北荒
    北荒 (楼主)
    2020-11-22 11:23

    As the keypress event isn't covered by any official specification, the actual behavior encountered when using it may differ across browsers, browser versions, and platforms.

    $(document).keydown(function(event) {
      if (event.keyCode || event.which === 13) {
        // Cancel the default action, if needed
        event.preventDefault();
        //call function, trigger events and everything tou want to dd . ex : Trigger the button element with a click
        $("#btn").trigger('click');
      }
    })
    
    

    I hope it would be useful!

提交回复
热议问题