forms - 对 HTML 表单的 action 属性使用空 URL 是一种好习惯吗? ( Action ="")

标签 forms web-standards string html

我想知道是否有人可以对使用空白 HTML 表单操作回发到当前页面给出“最佳实践”响应。

a post asking what a blank HTML form action does here和一些页面,如 this one建议很好,但我想知道人们的想法。

最佳答案

您能做的最好的事情就是完全省略 action 属性。如果您不填写,表单将被提交到文档的地址,即同一页。

也可以将其留空,任何实现 HTML's form submission algorithm 的浏览器都可以。会将其视为等同于文档的地址,这主要是因为浏览器目前是这样工作的:

8. Let action be the submitter element's action.

9. If action is the empty string, let action be the document's address.

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

这绝对适用于所有当前浏览器,但在某些旧浏览器中可能无法正常工作(browsers do weird things with an empty action="" attribute),这就是 the spec strongly discourages authors from leaving it empty 的原因:

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

关于forms - 对 HTML 表单的 action 属性使用空 URL 是一种好习惯吗? ( Action =""),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1131781/

相关文章:

javascript - 在Javascript中添加到对象时,如何使显示为数组的字符串返回数组

c++ - 将两个不同的 const 字符串传递给 std::string::insert 成员函数是未定义行为吗?

python - 谷歌应用程序引擎(Python): Using multiple forms on a single web page

JavaScript 无法从 HTML 表单正确运行

html - 表格突出显示和轮廓大小

javascript - 如何记住未提交的表单数据?

javascript - 重构一个使用 window.open 来使用 DOM 而不是 write() 的函数

html - 严格的文档类型 - 表单和输入元素

html - 无序列表和可访问性

java - 如何打印我的 Java 对象而不得到 "SomeType@2f92e0f4"?