表单上真的需要 action 属性吗?
这里说表单元素需要 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
该要求仅符合标准。完全有可能在页面上做任何你想做的事情而不遵循标准。如果您这样做,事情可能无法正常显示或工作,但很可能会。我们的目标是关注他们,我们的想法是,如果您关注他们,您的页面将始终正常运行;你不必担心任何事情。
是的,表单需要具有 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).
看起来HTML4 规范需要它。我怀疑有些浏览器会做你想做的“让事情变得更容易”。但我不建议依赖它。由于您处于未定义的行为,因此当表单在没有
操作
的情况下提交时,浏览器可以合理地决定不执行任何操作。通过将操作留空,您可以在遵循规范的同时获得所需的行为(因为它是相对的,空白表示当前页面):
正如 bazmegakapa 所提到的,HTML5 规范 似乎不需要
action
属性:有趣的是,这意味着在 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):
As mentioned by bazmegakapa, the HTML5 spec doesn't seem to require the
action
attribute: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).在 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.
如果在评估完所有按钮
formaction
和表单action
属性后,如果“action”仍然评估为“空字符串”,则来自 HTML5。 2 规范部分 4.10.21.3 第 8 点指出:当涉及到提交表格时,这就是您想要的。
If after all button
formaction
and formaction
attributes have been assessed, if "action" still evaluates as "empty string", then from HTML5.2 spec section 4.10.21.3 point 8 states:when it comes to submission of the form, which is what you wanted.