Làm thế nào để - Nút kích hoạt Nhấp vào Enter
Kích hoạt một nút bấm trên bàn phím "enter" bằng JavaScript.
Kích hoạt một nút Nhấp vào Enter
Nhấn phím "Enter" bên trong trường nhập để kích hoạt nút:
Thí dụ
// Get the input field
var input = document.getElementById("myInput");
// Execute a function when the user releases a key on the keyboard
input.addEventListener("keyup",
function(event) {
// Number 13 is the "Enter" key on the keyboard
if (event.keyCode ===
13) {
// Cancel the default action, if needed
event.preventDefault();
// Trigger the button element with a click
document.getElementById("myBtn").click();
}
});
Mẹo: Tìm hiểu thêm về thuộc tính event.keyCode trong Tài liệu tham khảo JavaScript của chúng tôi.