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

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

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

Cú pháp

Lấy giá trị chuỗi

inputURLObject.name

Thiết lập name

inputURLObject.name = 'Chuỗi_mới'

Ví dụ minh họa

Ô URL có name="Jack", value=example.com. Nhấn nút kiểm tra chủ sở hữu:

<!DOCTYPE html>
<html>
<head>
<title>Input URL 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>URL-name</legend>
    <label for="URLSelect">URL nhân viên: <input type="url" id="URLSelect" value="https://www.example.com" name="Jack"> </label> 
    <input type="button" onclick="kiemTraChu()" value="Ai là chủ?"> 
    <div id="divDisplay"></div> 
  </fieldset> 
</form> 
<script> 
  var divDisplay = document.getElementById("divDisplay"); 
  var inputURL = document.getElementById("URLSelect"); 
  function kiemTraChu() { 
    if(inputURL.value === 'https://www.example.com') 
      divDisplay.textContent = 'URL trên thuộc về ' + inputURL.name; 
    else 
      divDisplay.textContent = 'URL không thuộc ai!'; 
  } 
</script> 
</body> 
</html>

Kết quả

  • Ban đầu: ô URL example.com, name="Jack".
  • Nhấn Ai là chủ?: "URL trên thuộc về Jack".
  • Nếu đổi URL khác rồi nhấn: "URL không thuộc ai!".

Ý nghĩa: Thuộc tính name bắt buộc để server nhận diện dữ liệu form khi submit truyền thống (application/x-www-form-urlencoded).