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

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

Thuộc tính value của đối tượng Input DatetimeLocal lấy hoặc thiết lập giá trị ngày giờ cục bộ (chuỗi định dạng YYYY-MM-DDThh:mm) của trường datetime-local.

Cú pháp

Lấy giá trị (chuỗi)

inputDatetimeLocalObject.value

Thiết lập giá trị

inputDatetimeLocalObject.value = 'Chuỗi_ngày_giờ'

Ví dụ minh họa

Dịch chuyển ngày thi sang ngày sau khi không sẵn sàng:

<!DOCTYPE html>
<html>
<head>
<title>Input DatetimeLocal 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>Datetime-Local-value</legend>
    <label for="datetimeLocalSelect">Ngày giờ thi: <input type="datetime-local" id="datetimeLocalSelect" value="2022-10-29T09:30"> </label>
    <input type="button" onclick="updateValue()" value="Chưa sẵn sàng thi">
    <div id="divDisplay"></div>
  </fieldset>
</form>
<script>
  var divDisplay = document.getElementById("divDisplay");
  var inputDatetimeLocal = document.getElementById("datetimeLocalSelect");
  function updateValue() {
    inputDatetimeLocal.value = '2022-11-02T09:30';
    divDisplay.textContent = 'Ngày thi mới: ' + (inputDatetimeLocal.value).split('T')[0];
  }
</script>
</body>
</html>

Kết quả

Ban đầu hiển thị 29/10/2022. Nhấn nút cập nhật sang 02/11/2022 và hiển thị phần ngày.