HTML - method 屬性



HTML method 屬性用於定義提交表單時使用的 HTTP 方法。

method 屬性的值可以是 GET 或 POST,其中 'GET' 是預設方法。如果您沒有指定 method 屬性,則預設為 'GET' 方法。

語法

<form method = "value"></form>

其中 value 可以是 ‘GET’ 或 ‘POST’。

應用於

以下列出的元素允許使用 HTML method 屬性

元素 描述
<form> HTML <form> 標籤用於透過表單收集網站上的使用者輸入。

HTML method 屬性示例

以下示例將說明 HTML method 屬性,以及我們應該在哪裡以及如何使用此屬性!

使用 "get" 方法提交表單

在下面的示例中,我們使用表單元素的 HTML 'method' 屬性來定義提交表單時應使用哪種 HTTP 方法(即 GET 方法)。

<!DOCTYPE html>
<html lang="en">
<head>
   <title>HTML 'method' attribute</title>

</head>
<body>
   <!--HTML 'method' attribute-->
   <p>
      Example of the HTML 'method' attribute
   </p>
   <form method="GET">
      <h2>Login Page</h2>
      <label for="">
         Username
      </label>
      <input type="text" 
             name="uname">
      <br>
      <br>
      <label for="">
         Password
      </label>
      <input type="password" 
             name="password">
      <br>
      <br>
      <button>Login</button>
   </form>
</body>
</html>

使用 "post" 方法提交表單

以下是另一個 HTML ‘method’ 屬性示例。在這裡,我們建立一個包含兩個輸入欄位和一個按鈕的表單。然後,我們在表單元素中使用 method 屬性並將 method 值設定為 ‘POST’,以將表單資料傳送到伺服器。

<!DOCTYPE html>
<html lang="en">

<head>
   <title>HTML 'method' attribute</title>
</head>

<body>
   <!--HTML 'method' attribute-->
   <p>
      Example of the HTML 'method' attribute
   </p>
   <form method="POST">
      <h2>
         Login Page
      </h2>
      <label for="">
         Username
      </label>
      <input type="text" 
             name="uname">
      <br>
      <br>
      <label for="">
         Passwaord
      </label>
      <input type="password" 
             name="password">
      <br>
      <br>
      <button>Login</button>
   </form>
</body>

</html>

GET 和 POST 方法的區別

  • GET 方法用於向 Web 伺服器請求 URL 以獲取使用者提交的表單輸入資料。提交表單資料(值)後,這些值將顯示在 URL 中。
  • POST 方法用於在 HTTP 請求的主體中傳送或附加表單資料。提交表單後,表單資料(值)不會顯示在 URL 中。

支援的瀏覽器

屬性 Chrome Edge Firefox Safari Opera
method
html_attributes_reference.htm
廣告