How to detect pressing Enter on keyboard using jQuery?

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

    The whole point of jQuery is that you don't have to worry about browser differences. I am pretty sure you can safely go with enter being 13 in all browsers. So with that in mind, you can do this:

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

提交回复
热议问题