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

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

Thuộc tính name của đối tượng Input Time trong HTML DOM trả về hoặc thiết lập giá trị thuộc tính name của ô nhập giờ. Thuộc tính name dùng để xác định dữ liệu form khi submit lên server.

Cú pháp

Lấy giá trị chuỗi

inputTimeObject.name

Thiết lập name

inputTimeObject.name = 'Chuỗi_mới'

Ví dụ minh họa

Form đặt lịch hẹn: thay đổi tên bệnh viện (name attribute) và kiểm tra khi xác nhận:

<!DOCTYPE html>
<html>
<head>
<title>Input Time name</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-name</legend>
    <label for="TimeSelect">Hẹn tại Stephan:</label> 
    <input type="time" name="Stephan" id="TimeSelect" value="18:00"> 
    <input type="button" onclick="xacNhanHen()" value="Xác nhận"> 
    <input type="button" onclick="doiTen()" value="Đặt tại Dorcel's"> 
    <div id="divDisplay"></div> 
  </fieldset> 
</form> 
<script> 
  var labelDisplay = document.getElementsByTagName("label")[0]; 
  var divDisplay = document.getElementById("divDisplay"); 
  var inputTime = document.getElementById("TimeSelect"); 
  function xacNhanHen() { 
    if(inputTime.name === 'Stephan') 
      divDisplay.textContent = 'Bệnh viện Stephan hôm nay đóng cửa'; 
    else 
      divDisplay.textContent = 'Hẹn tại Bệnh viện Dorcel lúc: ' + inputTime.value; 
  } 
  function doiTen(){ 
    inputTime.name = 'Dorcel'; 
    labelDisplay.textContent = 'Hẹn tại ' + inputTime.name + ': '; 
  } 
</script> 
</body> 
</html>

Kết quả

  • Ban đầu: label "Hẹn tại Stephan:", name="Stephan".
  • Nhấn Xác nhận: "Bệnh viện Stephan hôm nay đóng cửa".
  • Nhấn Đặt tại Dorcel's: name đổi thành "Dorcel", label cập nhật.
  • Nhấn Xác nhận lại: "Hẹn tại Bệnh viện Dorcel lúc: 18:00".

Ý nghĩa: Thuộc tính name quan trọng khi submit form truyền thống (không dùng AJAX/Fetch) — server nhận dữ liệu theo cặp name=value.