HTML <button> formmethod 屬性

定義和用法

formmethod 屬性規定發送表單數據時使用的 HTTP 方法。此屬性覆蓋表單的 method 屬性。

formmethod 屬性僅用于 type="submit" 的按鈕。

表單數據可以作為 URL 變量發送(使用 method="get"),或作為 HTTP post 發送(使用 method="post")。

有關“get”方法的注意事項:

  • 它將表單數據以名稱/值對的形式附加到 URL
  • 這對于用戶希望將結果加為書簽的表單提交非常有用
  • URL中可以放置的數據量有限(因瀏覽器而異),因此無法確保所有表單數據都能正確傳輸。
  • 永遠不要使用“get”方法傳遞敏感信息!(密碼或其他敏感信息將顯示在瀏覽器的地址欄中)

有關“post”方法的注意事項:

  • 它將表單數據作為 HTTP POST 事務發送
  • 使用“post”方法提交的表單無法作為書簽保存
  • 與“get”方法相比,“post”方法更健壯和安全
  • 它沒有大小限制

實例

帶有兩個提交按鈕的表單。第一個提交按鈕使用 method="get" 提交表單數據,第二個提交按鈕使用 method="post" 提交表單數據:

<form action="/action_page.php" method="get">
  <label for="fname">名字:</label>
  <input type="text" id="fname" name="fname"><br><br>
  <label for="lname">姓氏:</label>
  <input type="text" id="lname" name="lname"><br><br>
  <button type="submit">提交</button>
  <button type="submit" formmethod="post">使用 POST 提交</button>
</form>

親自試一試

語法

<button type="submit" formmethod="get|post">

屬性值

描述
get 將表單數據附加到 URL:URL?name=value&name=value
post 將表單數據作為 HTTP post 事務發送。

瀏覽器支持

表中的數字注明了首個完全支持該屬性的瀏覽器版本。

Chrome Edge Firefox Safari Opera
Chrome Edge Firefox Safari Opera
9.0 10.0 4.0 5.1 15.0

注釋:formmethod 屬性是 HTML 5 中的新屬性。