คุณสมบัติ inputType ของ InputEvent ใช้สำหรับดึงประเภทของเหตุการณ์การป้อนข้อมูลที่ถูกกระตุ้น
ไวยากรณ์ (Syntax)
event.inputType
ตัวอย่างการใช้งาน
ตัวอย่างด้านล่างตรวจจับประเภทการกระทำ: พิมพ์ข้อความ, กด Backspace, หรือกด Delete
<!DOCTYPE html>
<html>
<head>
<title>InputEvent inputType</title>
<style>
form { width: 70%; margin: 0 auto; text-align: center; }
* { padding: 2px; margin: 5px; }
input[type="button"] { border-radius: 10px; }
</style>
</head>
<body>
<form>
<fieldset>
<legend>InputEvent-inputType</legend>
<label>Action teller: <input type="text" id="textSelect" oninput="getEventInputType(event)"> </label>
<div id="divDisplay"></div>
</fieldset>
</form>
<script>
var divDisplay = document.getElementById("divDisplay");
var textSelect = document.getElementById("textSelect");
function getEventInputType(InputEvent) {
if(InputEvent.inputType === 'insertText')
divDisplay.textContent = 'You are typing: ' + textSelect.value;
else if(InputEvent.inputType === 'deleteContentBackward')
divDisplay.textContent = 'You are using backspace key';
else if(InputEvent.inputType === 'deleteContentForward')
divDisplay.textContent = 'You are using delete key';
else
divDisplay.textContent = 'You are sitting idle, do something';
}
</script>
</body>
</html>
ผลลัพธ์
ก่อนพิมพ์: แสดง "You are sitting idle, do something"
ขณะพิมพ์: แสดง "You are typing: [ข้อความที่พิมพ์]"
กด Backspace: แสดง "You are using backspace key"
กด Delete: แสดง "You are using delete key"
หมายเหตุ: รูปภาพตัวอย่างจากต้นฉบับไม่ได้รวมอยู่ในเนื้อหานี้