表单上真的需要 action 属性吗?

发布于 2025-01-08 03:27:25 字数 244 浏览 0 评论 0原文

这里说表单元素需要 action 属性:

http:// /www.w3schools.com/tags/att_form_action.asp

但我看到即使我没有指定操作属性,表单也会被提交,并且表单会被提交到当前页面,这正是我想要的。

Here it says the action attribute is required on form elements:

http://www.w3schools.com/tags/att_form_action.asp

but I see that forms get submitted even if I don't specify an action attribute, and the form gets submitted to the current page which is exactly what I want.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

美煞众生 2025-01-15 03:27:25

该要求仅符合标准。完全有可能在页面上做任何你想做的事情而不遵循标准。如果您这样做,事情可能无法正常显示或工作,但很可能会。我们的目标是关注他们,我们的想法是,如果您关注他们,您的页面将始终正常运行;你不必担心任何事情。

是的,表单需要具有 HTML4 中的操作属性。如果未设置,浏览器可能会使用与向其提供空字符串相同的方法。您确实应该设置 action="" ,它是完全有效的 HTML4,遵循标准,并达到相同的精确结果。

在 HTML5 中,您实际上可以在提交按钮本身上指定一个操作。如果没有,它将使用表单的操作,如果未设置,则默认为空字符串(请注意,您无法在 HTML5 中显式地将操作设置为空字符串)。

The requirement is only by standards. It is perfectly possible to do whatever you want on a page and not follow standards. Things may not display or work correctly if you do that, but likely they will. The goal is to follow them, and the idea is that if you follow them, your page will always work; you don't have to worry about anything.

Yes, the form is required to have an action attribute in HTML4. If it's not set, the browser will likely use the same method as providing an empty string to it. You really should set action="" which is perfectly valid HTML4, follows standards, and achieves the same exact result.

In HTML5, you can actually specify an action on the submit button itself. If there isn't one, it uses the form's action and if that is not set, it defaults to the empty string (note you cannot explicitly set the action to an empty string in HTML5).

暮年慕年 2025-01-15 03:27:25

看起来HTML4 规范需要它。我怀疑有些浏览器会做你想做的“让事情变得更容易”。但我不建议依赖它。由于您处于未定义的行为,因此当表单在没有操作的情况下提交时,浏览器可以合理地决定不执行任何操作。

通过将操作留空,您可以在遵循规范的同时获得所需的行为(因为它是相对的,空白表示当前页面):

<form action="" ...>

正如 bazmegakapa 所提到的,HTML5 规范 似乎不需要 action 属性

action 和 formaction 内容属性(如果指定)必须具有一个值,该值是可能被空格包围的有效非空 URL。[强调已添加]

有趣的是,这意味着在 HTML5 中,< form action=""> 无效,但不清楚是否需要不带操作的表单才能工作(提交到当前页面)。

It looks like the HTML4 spec requires it. I suspect some browsers do what you want to "make things easier". I don't recommend relying it on though. Since you're in undefined behavior, a browser could reasonably decide to just do nothing when the form is submitted with no action.

You can get the behavior you want while following the spec by leaving the action blank (since it's relative, blank means the current page):

<form action="" ...>

As mentioned by bazmegakapa, the HTML5 spec doesn't seem to require the action attribute:

The action and formaction content attributes, if specified, must have a value that is a valid non-empty URL potentially surrounded by spaces.[emphasis added]

Interestingly, this means in HTML5, <form action=""> is not valid, but it's not clear if a form without an action is required to work (submit to the current page).

仙女山的月亮 2025-01-15 03:27:25

在 HTML 5 中,他们列出了不再需要的 action 属性: http://www.w3schools.com /html5/att_form_action.asp 符合 HTML 5 规范。

从技术上讲,这违反了 HTML 4 规范,但如果未指定任何操作,所有浏览器都会回发响应的发起者。我同意依赖它不是一个聪明的主意,但它确实有效。

In HTML 5 they list the action attribute as no longer required: http://www.w3schools.com/html5/att_form_action.asp which is in accordance with the HTML 5 specs.

Technically it's a violation of the HTML 4 spec, but all browsers will post back to the originator of the response if no action is specified. I would agree it's not a smart idea to rely on it but it does work.

分开我的手 2025-01-15 03:27:25

如果在评估完所有按钮 formaction 和表单 action 属性后,如果“action”仍然评估为“空字符串”,则来自 HTML5。 2 规范部分 4.10.21.3 第 8 点指出:

如果action是空字符串,则令action为文档的URL
形成文档。

当涉及到提交表格时,这就是您想要的。

If after all button formaction and form action attributes have been assessed, if "action" still evaluates as "empty string", then from HTML5.2 spec section 4.10.21.3 point 8 states:

If action is the empty string, let action be the document’s URL of the
form document.

when it comes to submission of the form, which is what you wanted.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文