Run Jquery method when enter key is pressed

帅比萌擦擦* 提交于 2019-12-01 06:41:48

You can bind a keypress event to the document and trigger() the handler on your button.

$(document).bind('keypress', function(e){
   if(e.which === 13) { // return
      $('#buttonid').trigger('click');
   }
});

This would fire whenever you press the return key on your site. You probably want it only to happen if someone uses return in your input form. You would just need to change the selector from document to any kind of selector that matches your control.

Ref.: .trigger()

$(document).keydown(function(e) {
   // test for the enter key
   if (e.keyCode == 13) {
      // Do your function
      myFunction();
   }
});
henning
$(".input_loginform").keypress(function(e){
    if (e.which == 13){
        e.preventDefault();
        alert("User pressed 'Enter' in a text input. sender form #"+this.form.id);
        postlogin("#"+this.form.id);
    }
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!