Thuộc tính form của đối tượng Input Time trả về tham chiếu đến đối tượng Form chứa ô nhập giờ này. Hữu ích khi cần truy xuất form cha từ bên trong phần tử con.
Cú pháp
Lấy tham chiếu form
inputTimeObject.form
Ví dụ minh họa
Ô time nằm trong form id="Physics". Lấy ID form thông qua thuộc tính form:
<!DOCTYPE html>
<html>
<head>
<title>Input Time form</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 id="Physics">
<fieldset>
<legend>Time-form</legend>
<label for="TimeSelect">Giờ thi: <input type="time" id="TimeSelect" value="14:00" disabled> </label>
<input type="button" onclick="layForm()" value="Kỳ thi nào?">
<div id="divDisplay"></div>
</fieldset>
</form>
<script>
var divDisplay = document.getElementById("divDisplay");
var inputTime = document.getElementById("TimeSelect");
function layForm() {
divDisplay.textContent = inputTime.form.id + ' thi bắt đầu từ ' + inputTime.value;
}
</script>
</body>
</html>
Kết quả
- Ban đầu: ô time 14:00 disabled.
- Nhấn Kỳ thi nào?: "Physics thi bắt đầu từ 14:00".
Ứng dụng: Khi có nhiều form trên trang, dùng input.form để biết input thuộc form nào, từ đó submit đúng form hoặc lấy dữ liệu form khác.