简体   繁体   English

表单元素上的action属性可以为空吗?

[英]Can the action attribute on a form element be empty?

As of HTML5, the widely used and recommended action="" is invalid HTML. 从HTML5开始,广泛使用和推荐的action=""是无效的HTML。

From the specs: 从规格:

The action and formaction content attributes, if specified, must have a value that is a valid URL. 如果指定了action和formaction内容属性,则必须具有一个有效URL值。

So what is the correct way to have the action attribute point to the current page? 那么,使action属性指向当前页面的正确方法是什么?

I am currently using 我目前正在使用

action="<php htmlspecialchars($_SERVER["REQUEST_URI"]); ?>"

It's true that the action= attribute may not be empty, as an empty string is not a valid URL. 确实, action=属性不能为空,因为空字符串不是有效的URL。 However, it is valid to simply leave it out, and let the browser figure it out. 但是,简单地将其保留,然后让浏览器找出它是有效的。

The browser will assume the current page. 浏览器将采用当前页面。

You can set value to '.' 您可以将值设置为“。” or do not set action attribute at all. 或根本不设置动作属性。

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

(I was surprise but it is really invalid to leave it blank, at least validator.w3.org is returning error). (我很惊讶,但是将其保留为空白确实是无效的,至少validator.w3.org返回了错误)。

不可以,在HTML5中,可以省略action属性 ,它默认为emoty字符串,表示对当前文档的引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM