How to trigger button click by pressing Enter key
Use JavaScript to trigger button click by pressing "enter" key on the keyboard.
Trigger button click by pressing Enter key
Please press the "enter" key in the input box to trigger the button:
Instance
// Get the input field var input = document.getElementById("myInput"); // Function executed when the user presses a key on the keyboard input.addEventListener("keypress", function(event) { // If the user presses the Enter key on the keyboard if (event.key === "Enter") { // If necessary, cancel the default operation event.preventDefault(); // I trigger the click event of the button element document.getElementById("myBtn").click(); } });
相关页面
参考手册:KeyboardEvent key 属性