Detect the Enter key in a text input field

后端 未结 10 717
粉色の甜心
粉色の甜心 2020-11-29 16:20

I\'m trying to do a function if enter is pressed while on specific input.

What I\'m I doing wrong?

$(document).keyup(function (e) {
    if ($(\".inpu         


        
10条回答
  •  一生所求
    2020-11-29 16:56

    The best way I found is using keydown ( the keyup doesn't work well for me).

    Note: I also disabled the form submit because usually when you like to do some actions when pressing Enter Key the only think you do not like is to submit the form :)

    $('input').keydown( function( event ) {
        if ( event.which === 13 ) {
            // Do something
            // Disable sending the related form
            event.preventDefault();
            return false;
        }
    });
    

提交回复
热议问题