jQuery 如何触发回车键

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17217892/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 18:39:24  来源:igfitidea点击:

How to trigger the enter keypress

javascriptjquery

提问by Derek Henderson

I need to know how to trigger an enter key on an input. That is, in response to some other event (not a keypress), I need to trigger a keypress of value 13.

我需要知道如何在输入上触发回车键。也就是说,为了响应其他一些事件(不是按键),我需要触发值为 13 的按键。

(Clarification, I do not want to trigger some event when the enter key is pressed. If I didn't know how to do that, I could find that that's been asked and answered several times on SO. I want the reverse. I am trying to develop a workaround that requires I emulate an 'enter' keypress.)

(澄清一下,我不想在按下 Enter 键时触发某些事件。如果我不知道该怎么做,我会发现已经在 SO 上多次询问并回答了这个问题。我想要相反的。我是试图开发一种需要我模拟“输入”按键的解决方法。)

回答by Mohammad Adil

You can do this -

你可以这样做 -

var e = $.Event( "keypress", { which: 13 } );
$('#yourInput').trigger(e);

回答by user1188867

 var e = $.Event( "keyup", { keyCode: 13 } );
    $('#yourInput').trigger(e);

Worked for me, instead of 'which', I used 'keyCode'

为我工作,而不是“哪个”,我使用了“keyCode”