為什麼 formaction 屬性不適用於
標籤之外?


我們可以讓 formaction 屬性在 <form> 標籤之外工作。formaction 屬性用於為一個表單指定多個提交 URL。提交表單時,web 瀏覽器首先檢查是否有 formaction 屬性。

如果沒有 formaction,web 瀏覽器會繼續查詢表單元素上的 action 屬性。

示例

下面是一個帶三個不同提交按鈕的 formaction 屬性的示例 −

<!DOCTYPE html>
<html>
   <head>
      <title>HTML formaction attribute</title>
   </head>

   <body>
      <form method="post">
         <input type = "text" name="name"/><br>
         <button type = "submit" formaction = "btn1.php">Button1</button>
         <button type = "submit" formaction = "btn2.php">Button2</button>
         <button type = "submit" formaction = "btn3.php">Button3</button>
      </form>
   </body>
</html>

是的,formaction 屬性不適用於 form 元素之外,但你仍然可以用以下方法讓它們正常工作 −

示例

您可以使用關聯的表單 id 值輕鬆放置按鈕並在表單外部使用 formaction 屬性。

<!DOCTYPE html>
<html>
   <head>
      <title>HTML formaction attribute</title>
   </head>

   <body>
      <form method="post" id="newForm">
         <input type="text" name="name"/>
      </form>

      <button type="submit" formaction="btn1.php" form="newForm">Button1</button>
      <button type="submit" formaction="btn2.php" form="newForm">Button2</button>
      <button type="submit" formaction="btn3.php" form="newForm">Button3</button>
   </body>
</html>

更新於: 15-Jun-2020

611 瀏覽量

啟動您的 職業生涯

完成課程後獲得認證

開始
廣告
© . All rights reserved.