HTML 表单的`name` 属性有什么意义?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8946320/
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
What's the point of HTML forms `name` attribute?
提问by Yarin
What's the point of the name
attribute on an HTML form? As far as I can tell, you can't read the form name on submission or do anything else with it. Does it serve a purpose?
name
HTML 表单上的属性有什么意义?据我所知,您无法在提交时读取表单名称或对其进行任何其他操作。它有用吗?
采纳答案by Quentin
In short, and probably oversimplifying a bit: It is used instead of id
for browsers that don't understand document.getElementById
.
简而言之,可能有点过于简单化了:它用于代替id
不理解document.getElementById
.
These days it serves no real purpose. It is a legacy from the early days of the browser wars before the use of name
to describe how to send control values when a form is submitted and id
to identify an element within the page was settled.
这些天它没有真正的目的。它是早期浏览器War的遗留物,name
用于描述提交表单时如何发送控件值以及id
确定页面中的元素。
回答by Matt Ball
回答by Marc B
Once you assign a name to an element, you can refer to that element via document.name_of_element
throughout your code. Doesn't work too tell when you've got multiple fields of the same name, but it does allow shortcuts like:
为元素分配名称后,您可以document.name_of_element
在整个代码中引用该元素。当您有多个同名字段时,它不会起作用,但它确实允许使用以下快捷方式:
<form name="myform" ...>
document.myform.submit();
instead of
代替
document.getElementsByName('myform')[0].submit();
回答by Sean the Bean
Here's what MDN has to say about it:
以下是 MDN 对此的看法:
name
The name of the form. In HTML 4, its use is deprecated (id
should be used instead). It must be unique among the forms in a document and not just an empty string in HTML 5.
name
表单的名称。在 HTML 4 中,它的使用已被弃用(id
应改为使用)。它在文档中的表单中必须是唯一的,而不仅仅是 HTML 5 中的空字符串。
(from https://developer.mozilla.org/en-US/docs/Web/HTML/Element/form#attr-name)
(来自https://developer.mozilla.org/en-US/docs/Web/HTML/Element/form#attr-name)
I find it slightly confusing that specifies that it must be unique, non-empty string in HTML 5 when it was deprecated in HTML 4. (I'd guess that requirement only applies if the name
attribute is specified at all?). But I think it's safe to say that any purpose it once served has been superseded by the id
attribute.
当它在 HTML 4 中被弃用时,我发现它在 HTML 5 中指定它必须是唯一的、非空的字符串时有点令人困惑。(我猜这个要求只适用name
于完全指定属性的情况?)。但我认为可以肯定地说,它曾经服务过的任何目的都已被该id
属性取代。
回答by codechimp
name
attribute is not completely redundant vis a vis id
. As aforementioned, it useful with <forms>
, but less known is that it can also be used with with any HTMLCollection
, such as the children
property of any DOM element.
name
与 vis 相比,属性并非完全多余id
。如前所述,它对 有用<forms>
,但鲜为人知的是它也可以与 any 一起使用HTMLCollection
,例如children
任何 DOM 元素的属性。
HTMLCollection
, in additional to be a array-like object, will have named properties commensurate with any named members (or the first occurrence in case of non-unique name). It is useful to retrieve specific named nodes.
HTMLCollection
,除了是一个类似数组的对象之外,还将具有与任何命名成员相称的命名属性(或在非唯一名称的情况下第一次出现)。检索特定的命名节点很有用。
For example, in the following example HTML:
例如,在以下示例 HTML 中:
<div id='person1'>
<span name='firstname'>John</span>
<span name='lastname'>Doe</span>
<span name='middlename'></span>
</div>
<div id='person2'>
<span name='firstname'>Jane</span>
<span name='lastname'>Doe</span>
<span name='middlename'></span>
</div>
by naming each child, one can quickly and efficiently retrieve a named element, such as lastname
, as such:
通过命名每个子元素,可以快速有效地检索命名元素,例如lastname
,如下所示:
document.getElementById('person1').children.namedItem('lastname')
...and if there is no risk of 'length' being the name of a member element, (being that length
is a reserved property of HTMLCollection
), a more terse notation may be used instead:
...如果不存在将 'length' 作为成员元素名称的风险(因为它length
是 的保留属性HTMLCollection
),则可以使用更简洁的表示法:
document.getElementById('person1').children.lastname
DOM Living Standard 2019 March 29
An HTMLCollection object is a collection of elements...
The namedItem(key) method, when invoked, must run these steps:
If key is the empty string, return null.
Return the first element in the collection for which at least one of the following is true: it has an ID which is key; it is in the HTML namespace and has a name attribute whose value is key;
HTMLCollection 对象是元素的集合...
namedItem(key) 方法在调用时必须运行以下步骤:
如果 key 是空字符串,则返回 null。
返回集合中至少满足以下条件之一的第一个元素:它的 ID 是键;它位于 HTML 命名空间中,并且有一个 name 属性,其值为 key;
回答by Jon Bray
You can use the name attribute as an "extra information" attribute - similarly as with a hidden input - but this keeps the extra information tied into the form, which makes it just a little simpler to read/access.
您可以将 name 属性用作“额外信息”属性 - 与隐藏输入类似 - 但这将额外信息绑定到表单中,这使得读取/访问更简单一些。