วิธีการ - ปุ่มทริกเกอร์ คลิกที่ปุ่ม Enter
ทริกเกอร์การคลิกปุ่มบนแป้นพิมพ์ "ป้อน" ด้วย JavaScript
ทริกเกอร์ปุ่มคลิกที่ปุ่ม Enter
กดปุ่ม "Enter" ภายในช่องใส่เพื่อเรียกปุ่ม:
ตัวอย่าง
// 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();
}
});
เคล็ดลับ:เรียนรู้เพิ่มเติมเกี่ยวกับ คุณสมบัติ event.keyCodeในการอ้างอิง JavaScript ของเรา