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

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

Thuộc tính max của đối tượng Input DatetimeLocal lấy hoặc thiết lập ngày giờ cục bộ tối đa cho phép chọn.

Cú pháp

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

inputDatetimeLocalObject.max

Thiết lập giá trị

inputDatetimeLocalObject.max = "YYYY-MM-DDThh:mm:ss"

Định dạng chuỗi

Thành phầnMô tả
YYYYNăm (ví dụ: 2006)
MMTháng (01-12, ví dụ: 06)
DDNgày (01-31, ví dụ: 17)
TPhân tách ngày/giờ
hhGiờ (00-23, ví dụ: 02)
mmPhút (00-59, ví dụ: 21)
ssGiây (00-59, ví dụ: 40)

Ví dụ minh họa

Gia hạn bảo hiểm bằng cách cập nhật max và đặt value égal max:

<!DOCTYPE html>
<html>
<head>
<title>Input DatetimeLocal max</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-max</legend>
    <label for="datetimeLocalSelect">Hết hạn bảo hiểm: <input type="datetime-local" id="datetimeLocalSelect" value="2019-07-01T23:59:59" max="2019-08-01T23:59:59" disabled> </label>
    <input type="button" onclick="renewInsurance()" value="Gia hạn bảo hiểm">
    <div id="divDisplay"></div>
  </fieldset>
</form>
<script>
  var divDisplay = document.getElementById("divDisplay");
  var inputDatetimeLocal = document.getElementById("datetimeLocalSelect");
  function renewInsurance() {
    inputDatetimeLocal.max = '2029-07-01T23:59:59';
    inputDatetimeLocal.value = inputDatetimeLocal.max;
    divDisplay.textContent = 'Bảo hiểm gia hạn: ' + inputDatetimeLocal.value;
  }
</script>
</body>
</html>

Kết quả

Ban đầu trường disabled hiển thị ngày hết hạn cũ. Nhấn "Gia hạn bảo hiểm" cập nhật max và value sang 10 năm sau.