简体   繁体   English

用 javascript 呼叫按键

[英]Call key press with javascript

I am trying to press tab and enter key through javascript/jquery on page load.我试图在页面加载时通过 javascript/jquery 按下制表符并输入键。

like:喜欢:

  if(event.keyCode == 13){
    $("#submit").click();
  } 

this would only work when user presses the enter key but I want the javascript to do this stuff for the user on page load.这仅在用户按下回车键时有效,但我希望 javascript 在页面加载时为用户执行此操作。

You can simulate a keypress with this code:您可以使用以下代码模拟按键:

function simulateKeyPress(character) {
  jQuery.event.trigger({ type : 'keypress', which : character.charCodeAt(0) });
}

thanks to this thread: Is it possible to simulate key press events programmatically?感谢这个线程: 是否可以以编程方式模拟按键事件?

but as Ghyath Serhal pointed out, there might be a better way than to simulate a keypress to then catch it by your same code.但正如 Ghyath Serhal 指出的那样,可能有比模拟按键然后用相同的代码捕获它更好的方法。

Check the below if it helps如果有帮助,请检查以下内容

<body onload="$('#submit').click();"></body>

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

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