[英]How to make firing "enter key" event in JavaScript?
I am working on something little app for sign-in automation.我正在开发一些用于登录自动化的小应用程序。
It is like filling in the sign-in id, password inputs, and then pressing enter to submit.就像填写登陆id,密码输入,然后回车提交。
I tried click()
, submit()
on the button element, but it's not working.我在按钮元素上尝试了
click()
、 submit()
,但它不起作用。 All I want is just fire the enter key event
after filling the inputs, but many source codes are about get the keyboard event
which I don't need to.我想要的只是在填充输入后触发
enter key event
,但许多源代码都是关于get the keyboard event
的。
Is it impossible that press enter key event??按回车键事件是不可能的吗??
Try this code:试试这个代码:
var element = document.getElementById(/* Element */);
element.addEventListener("keydown", fuction(e) {
if(e.key == "Enter") {
// Do something
}
});
You can use HTML form
for this.您可以为此使用 HTML
form
。 Just wrap your inputs inside form and set onsubmit
event.只需将您的输入包装在表单中并设置
onsubmit
事件。 After you fill up the email and pass, enter
will trigger the onsubmit
.填完email通过后
enter
会触发onsubmit
。
Here is a simple example:这是一个简单的例子:
<form onsubmit="myFunction()">
Enter name: <input type="text" name="fname">
<input type="submit" value="Submit">
</form>
<script>
function myFunction() {
alert("The form was submitted");
}
</script>
Hope this helps.希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.