表格上真的需要行动吗?

时间:2021-02-18 19:39:29

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.

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

3 个解决方案

#1


44  

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.

是的,表单需要在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中将操作显式设置为空字符串)。

#2


12  

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

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:

正如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]

动作和格式化内容属性(如果已指定)必须具有可能由空格包围的有效非空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中,

无效,但不清楚是否需要没有操作的表单(提交到当前页面)。

#3


5  

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.asp which is in accordance with the HTML 5 specs.

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

#1


44  

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.

是的,表单需要在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中将操作显式设置为空字符串)。

#2


12  

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

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:

正如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]

动作和格式化内容属性(如果已指定)必须具有可能由空格包围的有效非空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中,

无效,但不清楚是否需要没有操作的表单(提交到当前页面)。

#3


5  

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.asp which is in accordance with the HTML 5 specs.

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