How to detect pressing Enter on keyboard using jQuery?

后端 未结 18 2528
有刺的猬
有刺的猬 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:36

    In some cases, you may need to suppress the ENTER key for a certain area of a page but not for other areas of a page, like the page below that contains a header

    with a SEARCH field.

    It took me a bit to figure out how to do this, and I am posting this simple yet complete example up here for the community.

    
    
    
      Test Script
      
      
    
      
        

    Link to JSFiddle Playground: The [Submit] button does not do anything, but pressing ENTER from one of the Text Box controls will not submit the form.

提交回复
热议问题