Html 带有 action="" 的表单

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/415234/
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:57:37  来源:igfitidea点击:

Forms with action=""

htmlforms

提问by nickf

I just found out (the hard way), that when you have a HTML form with action="", Webkit browsers treat it differently to Firefox and Internet Explorer.

我刚刚发现(艰难的方式),当您有一个带有 的 HTML 表单时action="",Webkit 浏览器将其处理方式与 Firefox 和 Internet Explorer 不同。

In FF and IE, these two form tags are equivalent:

在 FF 和 IE 中,这两个表单标签是等价的:

<form method="post" action="">

<form method="post">

They will both submit the form back to the same page. Safari and Chrome however will send that first form to the default page (index.php, or whatever) - the second form works the same as FF/IE.

他们都会将表单提交回同一页面。然而,Safari 和 Chrome 会将第一个表单发送到默认页面(index.php 或其他页面)——第二个表单的工作方式与 FF/IE 相同。

I've quickly hacked my code so that anywhere where it would normally print an empty action, it doesn't add an action attribute at all.

我很快就破解了我的代码,以便在通常会打印空操作的任何地方,它根本不添加操作属性。

This seems very messy and not the best way to be doing things. Can anyone suggest a better method? Also, can anyone enlighten me about why Webkit would do such a thing?

这看起来很混乱,并不是最好的做事方式。任何人都可以提出更好的方法吗?另外,有人能告诉我为什么 Webkit 会做这样的事情吗?

回答by staticsan

I usually use

我通常使用

<form method='POST' action='?'>

This means the current URL but with no parameters.

这意味着当前的 URL 但没有参数。

回答by Gumbo

The actionattribute is requiredbut you can specify an empty URI reference that referes to the current URI:

action属性是必需的,但您可以指定引用当前 URI 的空 URI 引用:

<form method="POST" action="">


Edit????Ok, this actually is a filed bug of WebKit 528+ (see Bug 19884) when an empty URI is used with a specified base URI using the BASEelement. In that case WebKit takes the base URI instead of resolving the empty URI from the base URI.

编辑????好的,这实际上是WebKit 528+(参见Bug 19884)的一个归档错误,当一个空URI 与使用BASE元素的指定基本URI 一起使用时。在这种情况下,WebKit 会使用基本 URI,而不是从基本 URI 解析空 URI。

But this is correct behavior according to RFC 3986:

根据 RFC 3986,这是正确的行为

5.1. Establishing a Base URI

The term "relative" implies that a "base URI" exists against which the relative reference is applied. […]

The base URI of a reference can be established in one of four ways, discussed below in order of precedence. The order of precedence can be thought of in terms of layers, where the innermost defined base URI has the highest precedence. This can be visualized graphically as follows:

  .----------------------------------------------------------.
  |  .----------------------------------------------------.  |
  |  |  .----------------------------------------------.  |  |
  |  |  |  .----------------------------------------.  |  |  |
  |  |  |  |  .----------------------------------.  |  |  |  |
  |  |  |  |  |       <relative-reference>       |  |  |  |  |
  |  |  |  |  `----------------------------------'  |  |  |  |
  |  |  |  | (5.1.1) Base URI embedded in content   |  |  |  |
  |  |  |  `----------------------------------------'  |  |  |
  |  |  | (5.1.2) Base URI of the encapsulating entity |  |  |
  |  |  |         (message, representation, or none)   |  |  |
  |  |  `----------------------------------------------'  |  |
  |  | (5.1.3) URI used to retrieve the entity            |  |
  |  `----------------------------------------------------'  |
  | (5.1.4) Default Base URI (application-dependent)         |
  `----------------------------------------------------------'

5.1. 建立基础 URI

术语“相对”意味着存在一个“基本 URI”,相对引用适用于该“基本 URI”。[…]

引用的基本 URI 可以通过以下四种方式之一建立,按优先级顺序讨论。可以从层的角度考虑优先顺序,其中最里面定义的基本 URI 具有最高优先级。这可以用图形表示如下:

  .----------------------------------------------------------.
  |  .----------------------------------------------------.  |
  |  |  .----------------------------------------------.  |  |
  |  |  |  .----------------------------------------.  |  |  |
  |  |  |  |  .----------------------------------.  |  |  |  |
  |  |  |  |  |       <relative-reference>       |  |  |  |  |
  |  |  |  |  `----------------------------------'  |  |  |  |
  |  |  |  | (5.1.1) Base URI embedded in content   |  |  |  |
  |  |  |  `----------------------------------------'  |  |  |
  |  |  | (5.1.2) Base URI of the encapsulating entity |  |  |
  |  |  |         (message, representation, or none)   |  |  |
  |  |  `----------------------------------------------'  |  |
  |  | (5.1.3) URI used to retrieve the entity            |  |
  |  `----------------------------------------------------'  |
  | (5.1.4) Default Base URI (application-dependent)         |
  `----------------------------------------------------------'

In this case the BASEelement with hrefattribute is a base URI embedded in content. And base URI embedded in contenthas a higher precedence than URI used to retrieve the entity. So WebKit's behavior is actually the expected behavior according to RFC 3986.

在这种情况下,BASE具有href属性的元素是嵌入在 content 中基本 URI。和嵌入在内容基础URI具有更高的优先级比URI用于检索实体。所以 WebKit 的行为实际上是根据 RFC 3986 的预期行为。

But in HTML 5 this behavior of an empty URI in form's action(still a draft) differs from RFC 3986:

但是在 HTML 5 中,form'saction(仍然是草稿)中的空 URI 的行为与 RFC 3986 不同:

If actionis the empty string, let actionbe the document's address.

Note: This step is a willful violationof RFC 3986, which would require base URL processing here. This violation is motivated by a desire for compatibility with legacy content. [RFC3986]

如果action是空字符串,则让action文档的地址

注意:此步骤故意违反RFC 3986,此处需要处理基本 URL。这种违反的动机是希望与遗留内容兼容。[RFC3986]

Frankly, an HTML comment after this note in the source code reads:

坦率地说,源代码中此注释后的 HTML 注释如下:

<!-- Don't ask me why. But that's what IE does. It even treats
action="" differently from action=" " or action="#" (the latter
two resolve to the base URL, the first one resolves to the doc
URL). And other browsers concur. It is even required, see e.g.
  http://bugs.webkit.org/show_bug.cgi?id=7763
  https://bugzilla.mozilla.org/show_bug.cgi?id=297761
-->
<!-- Don't ask me why. But that's what IE does. It even treats
action="" differently from action=" " or action="#" (the latter
two resolve to the base URL, the first one resolves to the doc
URL). And other browsers concur. It is even required, see e.g.
  http://bugs.webkit.org/show_bug.cgi?id=7763
  https://bugzilla.mozilla.org/show_bug.cgi?id=297761
-->

So this is rather a bug originated from Internet Explorer that became a de-facto standard.

所以这是一个源自 Internet Explorer 的错误,它成为了事实上的标准。

回答by Eran Galperin

The best way in my opinion would be not to omit the action attribute (which would not validate) but to specify the actual action for the form. Is there a reason you are not specifying the action?

我认为最好的方法不是省略 action 属性(不会验证),而是指定表单的实际操作。您是否有未指定操作的原因?

回答by alex

I've always used (in PHP)

我一直使用(在 PHP 中)

<form method="post" action="<?php echo htmlspecialchars($_SERVER['REQUEST_URI']); ?>">

To get my forms to submit to themselves.

让我的表格提交给自己。