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

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

Thuộc tính form của đối tượng Input URL (<input type="url">) trong HTML DOM trả về tham chiếu đến đối tượng Form chứa ô nhập URL. Dùng để truy xuất thông tin form cha (id, action, method...) từ phần tử con.

Cú pháp

Lấy tham chiếu form

inputURLObject.form

Ví dụ minh họa

Form id="Larry Page" chứa ô URL. Nhấn nút lấy tên co-founder từ form.id:

<!DOCTYPE html>
<html>
<head>
<title>Input URL form</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 id="Larry Page">
  <fieldset>
    <legend>URL-form</legend>
    <label for="URLSelect">URL: <input type="url" id="URLSelect" size="25"> </label> 
    <input type="button" onclick="layForm()" value="Lấy Co-founder"> 
    <div id="divDisplay"></div> 
  </fieldset> 
</form> 
<script> 
  var divDisplay = document.getElementById("divDisplay"); 
  var inputURL = document.getElementById("URLSelect"); 
  function layForm() { 
    if(inputURL.value !== '') 
      divDisplay.textContent = 'Co-founder: ' + inputURL.form.id; 
    else 
      divDisplay.textContent = 'Vui lòng nhập URL hợp lệ'; 
  } 
</script> 
</body> 
</html>

Kết quả

  • Ban đầu: ô URL trống.
  • Nhập URL (ví dụ https://google.com), nhấn Lấy Co-founder: "Co-founder: Larry Page".
  • Không nhập, nhấn nút: "Vui lòng nhập URL hợp lệ".

Ứng dụng: Xác định form chứa input khi có nhiều form, hoặc submit form qua inputURL.form.submit().