Html 真的需要对表格采取行动吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9401521/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 22:43:20  来源:igfitidea点击:

Is action really required on forms?

formshtmlhtml-validation

提问by Alex

Here it says it's required

这里说它是必需的

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

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.

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

回答by animuson

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 alwayswork; you don't have to worry about anything.

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

Yes, the form is requiredto 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.

是的,表单需要在 HTML4 中具有 action 属性。如果未设置,浏览器可能会使用与向其提供空字符串相同的方法。你真的应该设置action=""哪个是完全有效的 HTML4,遵循标准,并实现完全相同的结果。

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).

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

回答by Brendan Long

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.

看起来HTML4 规范需要它。我怀疑有些浏览器会做你想做的“让事情变得更容易”的事情。我不建议依赖它。由于您处于未定义的行为中,浏览器可以合理地决定在表单提交时不做任何事情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 actionattribute:

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

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]

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

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).

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

回答by swannee

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.

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

EDIT: As it has been pointed out to me that this question is tagged as HTML 5:In HTML 5 they list the action attribute as no longer required: http://www.w3schools.com/html5/att_form_action.aspwhich is in accordance with the HTML 5 specs.

编辑:由于它已经向我指出,这个问题被标记为HTML 5:在HTML 5,他们列为不再需要的action属性: http://www.w3schools.com/html5/att_form_action.asp这是在符合 HTML 5 规范。

回答by Neil Moss

// thread resurrection alert

// 线程复活提醒

To extend upon animuson's answer...

扩展animuson的回答......

If after all button formactionand form actionattributes have been assessed, if "action" still evaluatesas "empty string", then from HTML5.2 spec section 4.10.21.3point 8 states:

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

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

如果 action 为空字符串,则让 action 为表单文档的文档 URL。

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

在提交表单时,这正是您想要的。