Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> HTML

Thuộc tính value của HTML DOM Input Time

Thuộc tính value của đối tượng Input Time trong HTML DOM trả về hoặc thiết lập giá trị chuỗi của ô nhập giờ (<input type="time">). Định dạng giá trị: hh:mm hoặc hh:mm:ss hoặc hh:mm:ss.ms.

Cú pháp

Lấy giá trị chuỗi

inputTimeObject.value

Thiết lập giá trị chuỗi

inputTimeObject.value = 'Chuỗi_giờ'

Ví dụ minh họa

Trò chơi đoán giờ: ô time mặc định 00:00. Nhập giờ, nhấn "Đoán" kiểm tra có đúng 18:30 không:

<!DOCTYPE html>
<html>
<head>
<title>Input Time value</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>Time-value</legend>
    <label for="TimeSelect">Giờ: </label> 
    <input type="time" id="TimeSelect" value="00:00"> 
    <input type="button" onclick="kiemTraDoan()" value="Đoán"> 
    <div id="divDisplay"></div> 
  </fieldset> 
</form> 
<script> 
  var divDisplay = document.getElementById("divDisplay"); 
  var inputTime = document.getElementById("TimeSelect"); 
  divDisplay.textContent = 'Đoán xem sự kiện bắt đầu lúc mấy giờ?'; 
  function kiemTraDoan() { 
    if (inputTime.value === '18:30') 
      divDisplay.textContent = 'Chính xác! Sự kiện bắt đầu lúc ' + inputTime.value; 
    else 
      divDisplay.textContent = 'Sai rồi, thử lại nhé!'; 
  } 
</script> 
</body> 
</html>

Kết quả

  • Nhập sai giờ (ví dụ 10:00), nhấn Đoán: "Sai rồi, thử lại nhé!".
  • Nhập đúng 18:30, nhấn Đoán: "Chính xác! Sự kiện bắt đầu lúc 18:30".

Lưu ý: Giá trị value luôn ở định dạng 24h (hh:mm) bất kể locale. Khi submit form, giá trị gửi lên server cũng theo định dạng này.