📜  如何在 HTML 中使用 formaction 属性?(1)

📅  最后修改于: 2023-12-03 15:08:37.684000             🧑  作者: Mango

如何在 HTML 中使用 formaction 属性?

HTML formaction 属性用于在一个表单中指定提交的 URL。本文将介绍如何在 HTML 中使用 formaction 属性。

语法

formaction 属性可以在

<input type="submit" value="Submit" formaction="submit_url">
<button type="submit" formaction="submit_url">Submit</button>
示例

下面是一个简单的示例,演示了如何在 HTML 中使用 formaction 属性:

<!DOCTYPE html>
<html>
<head>
	<title>HTML formaction 属性示例</title>
</head>
<body>

	<form action="https://www.example.com/form_handler.php" method="POST">
		<input type="text" name="username" placeholder="用户名" required>
		<input type="password" name="password" placeholder="密码" required>
		<input type="submit" value="提交" formaction="https://www.example.com/submit_handler.php">
	</form>

</body>
</html>

在上面的例子中,我们创建了一个表单,其中包含一个用户名文本框、一个密码文本框和一个提交按钮。当用户单击提交按钮时,表单会提交到 https://www.example.com/submit_handler.php,而不是默认的表单处理器(https://www.example.com/form_handler.php)。

结论

通过使用 HTML formaction 属性,您可以轻松地指定在表单提交时要使用的 URL。这对于处理多个表单处理程序非常有用,因为您不必在每个处理程序的脚本中编写额外的代码来检测提交来源。