jQuery - Target button with enter key when input field is focused

后端 未结 5 1931
无人共我
无人共我 2021-02-01 04:33

I have an input field and a login button. I want to allow users to hit the \"enter\" key to click the button when the input field is focused. How would I do this with jQuery?<

5条回答
  •  余生分开走
    2021-02-01 05:13

    $('#loginUserID').keypress(function(event){
      if(event.keyCode == 13){
        $('#loginBtn').click();
      }
    });
    

    Demo: http://jsfiddle.net/Bhf5a/

提交回复
热议问题