我想知道是否有人可以对使用空白 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
andformaction
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/