當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


HTML DOM Form用法及代碼示例


HTML DOM中的Form對象用於表示HTML <form>元素。此標記用於設置或獲取<form>元素的屬性。可以使用getElementById()方法訪問此元素。

用法:

document.getElementById("Form_ID");

該Form_ID分配給HTML <form>元素。

示例1:使用document.getElementById(“myForm”).id返回“form id”;

<!DOCTYPE html> 
<html> 
  
<head> 
    <title> 
        HTML DOM Form Object 
    </title> 
</head> 
  
<body style="text-align:center;"> 
  
    <h1 style="color:green;">   
            GeeksForGeeks   
        </h1> 
  
    <h2>DOM Form Object</h2> 
  
    <form align="center" 
          id="myForm" 
          method="GET" 
          target="_blank"
          action="/action_page.php"> 
        First name:
        <br> 
        <input type="text"
               name="fname" 
               placeholder="FName"> 
        <br> 
        <br> Last name:
        <br> 
        <input type="text" 
               name="lname" 
               placeholder="LName"> 
        <br> 
        <br> 
    </form> 
    <button onclick="myGeeks()"> 
        Submit 
    </button> 
    <p id="Geek_p" 
       style="color:green;  
              font-size:30px;"> 
    </p> 
  
    <script> 
        function myGeeks() { 
            // Accessing form element. 
            var txt = document.getElementById( 
              "myForm").id; 
            
            document.getElementById( 
              "Geek_p").innerHTML = txt; 
        } 
    </script> 
</body> 
  
</html>

輸出



  • 在單擊按鈕之前:
  • 單擊按鈕後:

示例-2:使用document.getElementById(“Form_ID”).target;以表單形式返回目標屬性的值。

<!DOCTYPE html> 
<html> 
  
<head> 
    <title> 
        HTML DOM Form Object 
    </title> 
</head> 
  
<body style="text-align:center;"> 
  
    <h1 style="color:green;">   
            GeeksForGeeks   
        </h1> 
  
    <h2>DOM Form Object</h2> 
  
    <form align="center" 
          id="myForm"
          method="GET" 
          target="_blank"
          action="/action_page.php"> 
        First name:
        <br> 
        <input type="text"
               name="fname" 
               placeholder="FName"> 
        <br> 
        <br> Last name:
        <br> 
        <input type="text" 
               name="lname" 
               placeholder="LName"> 
        <br> 
        <br> 
    </form> 
    <button onclick="myGeeks()"> 
        Submit 
    </button> 
    <p id="Geek_p" 
       style="color:green;  
              font-size:30px;"> 
    </p> 
  
    <script> 
        function myGeeks() { 
            
            //  Accessing form. 
            var txt = document.getElementById( 
              "myForm").target; 
            
            
            document.getElementById( 
              "Geek_p").innerHTML = txt; 
        } 
    </script> 
</body> 
  
</html>

輸出

  • 在單擊按鈕之前:
  • 單擊按鈕後:

示例3:使用document.getElementById(“Form_ID”).length;返回表單中的元素數。

<!DOCTYPE html> 
<html> 
  
<head> 
    <title> 
        HTML DOM Form Object 
    </title> 
</head> 
  
<body style="text-align:center;"> 
  
    <h1 style="color:green;">   
            GeeksForGeeks   
        </h1> 
  
    <h2>DOM Form Object</h2> 
  
    <form align="center"
          id="myForm" 
          method="GET" 
          target="_blank"
          action="/action_page.php"> 
        First name:
        <br> 
        <input type="text"
               name="fname" 
               placeholder="FName"> 
        <br> 
        <br> Last name:
        <br> 
        <input type="text"
               name="lname" 
               placeholder="LName"> 
        <br> 
        <br> 
    </form> 
    <button onclick="myGeeks()"> 
        Submit 
    </button> 
    <p id="Geek_p"
       style="color:green; 
              font-size:30px;"> 
    </p> 
  
    <script> 
        function myGeeks() { 
            
          //Accessing form element. 
          var txt = document.getElementById( 
            "myForm").length; 
            
            document.getElementById( 
              "Geek_p").innerHTML = txt; 
        } 
    </script> 
</body> 
  
</html>

輸出

  • 在單擊按鈕之前:
  • 單擊按鈕後:

支持的瀏覽器:

  • 穀歌瀏覽器
  • 火狐瀏覽器
  • Edge
  • Safari
  • Opera

相關用法


注:本文由純淨天空篩選整理自PranchalKatiyar大神的英文原創作品 HTML | DOM Form Object。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。