Html 当表单操作属性为“#”(数字/磅符号/符号/字符)时,这意味着什么?

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

What does it mean when the form action attribute is "#" (number/pound symbol/sign/character)?

htmlforms

提问by XP1

What does it mean when the form actionattribute is "#" (number/pound symbol/sign/character)?

当表单action属性为“#”(数字/磅符号/符号/字符)时,这意味着什么?

What happens when a form input's formactionattribute is set to "#"? Does this prevent the input from being submitted to the server?

当表单输入的formaction属性设置为“#”时会发生什么?这是否会阻止输入提交到服务器?

<form method="GET" action="example.php">
    <input type="text" size="20" name="text1" value="text1" formaction="#"/>
    <input type="text" size="20" name="text2" value="text2"/>
    <input type="submit" value="Submit"/>
</form>

采纳答案by Jukka K. Korpela

The meaning of #as a URL reference (whether as actionor formactionattribute value or otherwise) is a reference to the start of the current base document. The base document is the current document, unless a <base href=...>tag has been set.

的含义#如URL引用(无论是作为actionformaction属性值或以其它方式)是将当前的基础文档的开始的引用。基础文档是当前文档,除非<base href=...>已设置标签。

What happens depends on the situation. Typically, the browser requests for the page again with a query part in the URL (and the page is loaded again, which may imply that client-side scripts are run), but if the same query had been used earlier, the browser probably uses its cache. Moreover, as the start of the document is referred to, focus on any form element is lost and the page may scroll backwards.

会发生什么取决于情况。通常,浏览器在 URL 中再次请求带有查询部分的页面(并且再次加载页面,这可能意味着运行客户端脚本),但如果之前使用过相同的查询,浏览器可能会使用它的缓存。此外,当引用文档的开头时,任何表单元素的焦点都会丢失,页面可能会向后滚动。

So although #is fairly common in some coding styles, it is not reliable; its purpose is better achieved using client-side event handlers.

因此,尽管#在某些编码风格中相当普遍,但它并不可靠;使用客户端事件处理程序可以更好地实现其目的。

The formactionattribute has a meaning only for submit buttons. A text input element does not constitute a submit button, even though it may trigger form submission, so here the attribute is ignored.

formaction属性仅对提交按钮有意义。文本输入元素不构成提交按钮,即使它可能会触发表单提交,因此这里忽略该属性。

回答by Vadim Gulyakin

The form will submit to itself (current URL). I think it is the same as empty action.

表单将提交给自身(当前 URL)。我认为这与空操作相同。

Also, can be useful if action is going to be changed by javascript at later time.

此外,如果稍后将通过 javascript 更改操作,则可能很有用。

回答by jocken

Explained on w3schools: http://www.w3schools.com/html5/html5_form_attributes.asp

在 w3schools 上解释:http: //www.w3schools.com/html5/html5_form_attributes.asp

Form Override Attributes

The form override attributes allow you to override some of the attributes set for the form element.

The form override attributes are:

formaction - Overrides the form action attribute
formenctype - Overrides the form enctype attribute
formmethod - Overrides the form method attribute
formnovalidate - Overrides the form novalidate attribute
formtarget - Overrides the form target attribute
Note: The form override attributes works with the following types:
submit and image.

表单覆盖属性

表单覆盖属性允许您覆盖为表单元素设置的一些属性。

表单覆盖属性是:

formaction - 覆盖表单操作属性
formenctype - 覆盖表单 enctype 属性
formmethod - 覆盖表单方法属性
formnovalidate - 覆盖表单 novalidate 属性
formtarget - 覆盖表单目标属性
注意:表单覆盖属性适用于以下类型:
提交和图像.

<form action="demo_form.asp" method="get" id="user_form">
E-mail: <input type="email" name="userid" /><br />
<input type="submit" value="Submit" />
<br />
<input type="submit" formaction="demo_admin.asp" value="Submit as admin" />
<br />
<input type="submit" formnovalidate="true"
value="Submit without validation" />
<br />
</form>

So yes, you are absolutely correct that it overrides the action, but it only overrides on input type submit and image, not on text. So you can have 2 different submitbuttons in the same form but can lead to different types of validaton. That's what I would use it for.

所以是的,您完全正确,它覆盖了操作,但它只覆盖输入类型提交和图像,而不覆盖文本。所以你可以在同一个表单中有 2 个不同的提交按钮,但可能会导致不同类型的验证。这就是我要用它的目的。

So a #would put the action on the same page rather than another one.

因此,#会将操作放在同一页面而不是另一个页面上。