为什么 formaction 属性在 form 标签之外不起作用?

htmlweb developmentfront end technology

我们可以让 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 属性在表单元素之外不起作用,但您仍然可以通过以下方式让它们正常工作 −

示例

您可以轻松地在表单外部放置按钮并使用 formaction 属性,并使用关联的表单 ID 值。

<!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>

相关文章


有用资源