Cách thực hiện - Nút kích hoạt Nhấp vào Enter
Kích hoạt nút bấm trên bàn phím "enter" bằng JavaScript.
Kích hoạt một nút Bấm vào Enter
Nhấn phím "Enter" bên trong trường nhập để kích hoạt nút:
Ví dụ
// Get the input field
var input = document.getElementById("myInput");
// Execute a function 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") {
// Cancel the default action, if needed
event.preventDefault();
// Trigger the button element with a click
document.getElementById("myBtn").click();
}
});
Hãy tự mình thử »Mẹo: Tìm hiểu thêm về Thuộc tính khóa KeyboardEvent trong Tài liệu tham khảo JavaScript của chúng tôi.