简体   繁体   English

如何触发浏览器的实际“ Enter”按键事件?

[英]How to fire the actual “Enter” keypress event of the browser?

I know it is possible to use JQuery.Event to simulate "Enter" keypress. 我知道可以使用JQuery.Event模拟“ Enter”按键。 Something like this does that - 像这样的事情-

var event = jQuery.Event( "keydown" );
event.keyCode = 13
$("input").trigger( event );

However, this does not fire the browser default behavior when the actual "Enter" key is pressed. 但是,当按下实际的“ Enter”键时,这不会触发浏览器的默认行为。 For example, going to next line if the cursor is in textarea or submitting the form when the cursor is in input tag. 例如,如果光标在textarea ,则转到下一行;当光标在input标签中时,提交表单。

I want to know if it is possible to trigger or simulate the actual "Enter" keypress event/behavior. 我想知道是否有可能触发或模拟实际的“ Enter”按键事件/行为。

Try this : Js fiddle 试试这个: Js提琴

var e = jQuery.Event("keypress");
e.which = 13 
$("#test").keypress(function(){
   alert('keypress triggered')
}).trigger(e)

Do you want some thing similar this one? 您想要类似的东西吗?

 $(function(){
   $(document).keypress(function(event) {
      if(event.which == 13) {
      console.log("Dude! You hit Enter Key!!");
     }
   });
 });

you can use this: 您可以使用此:

$("body").delegate("input", "keypress",function(e){
  if(e.which == 13){
  }
}

it's work for all input . 它适用于所有输入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM