名称属性在 HTML 文档中必须是唯一的吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5518458/
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
Does a name attribute have to be unique in a HTML document?
提问by Vilx-
I remember reading in the spec once that both the id
attribute and the name
attribute share the same namespace and have to be unique. Henceforth I've always tried to fulfill this requirement in my applications, dreading even to give the same id
and name
to the same element.
我记得有一次在规范中读到id
属性和name
属性共享相同的命名空间并且必须是唯一的。从今以后,我一直试图在我的应用程序中满足这个要求,甚至害怕给相同的元素id
和name
相同的元素。
But lately I've started working with ASP.NET MVC 3, and it (like PHP) can use the same name
attribute on several input controls to form a collection of values at server-side. I tried to look up the relevant section in the spec - but failed to find it. Perhaps I have misunderstood something then, or read the wrong documentation?
但是最近我开始使用 ASP.NET MVC 3,它(如 PHP)可以name
在多个输入控件上使用相同的属性来形成服务器端的值集合。我试图在规范中查找相关部分 - 但未能找到它。也许我当时误解了一些东西,或者阅读了错误的文档?
How is it then? I want to produce as valid HTML as possible (both 4.01 and 5 in different apps). Can I use this trick without fear? Or would I be violating something and should better stick to unique values?
那怎么样?我想生成尽可能有效的 HTML(不同应用程序中的 4.01 和 5)。我可以无所畏惧地使用这个技巧吗?或者我是否会违反某些内容而应该更好地坚持独特的价值观?
采纳答案by Kolten
The name
attribute is only valid on the <form>
and form elements (<input>
,<textarea>
and <select>
). It's used to specify the name
to associate with the name/value pair that is submitted on a form post.
该name
属性仅对<form>
和 表单元素(<input>
、<textarea>
和<select>
)有效。它用于指定name
与在表单帖子上提交的名称/值对相关联的 。
For example:
例如:
<input type="checkbox" name="foo" value="1" />
if checked will submit foo=1
. In the DOM you can reference form elements from the form.elements
collection by specifying the name
as the index. If name
is not unique, the collection returns an array of elements rather than the element. Modern DOM's support looking up form elements by name as:
如果选中将提交foo=1
。在 DOM 中,您可以form.elements
通过将 指定name
为索引来引用集合中的表单元素。如果name
不是唯一的,则集合返回一个元素数组而不是元素。现代 DOM 支持按名称查找表单元素:
document.getElementsByName(nameValue)
note: it always returns an array even if only one element is found.
注意:即使只找到一个元素,它也总是返回一个数组。
id
attribute is from the XML world, and is a unique id for any node, not just form elements. Unlike the name
attribute it is valid on any HTML node. Also like the name
attribute, it must follow the valid identifier rules. The identifier should start with an alpha, and only contain alpha ([a-zA-Z]
), numbers, hyphen, underscore and colons (note ASP.NET breaks this rule by starting reserved IDs with a underscore - thus they will always fail an HTML/XML lint - actually some proxies strip them). To find any HTML element by id
you use:
id
属性来自 XML 世界,是任何节点的唯一 id,而不仅仅是表单元素。与name
属性不同,它在任何 HTML 节点上都有效。也像name
属性一样,它必须遵循有效的标识符规则。标识符应该以字母开头,并且只包含字母 ( [a-zA-Z]
)、数字、连字符、下划线和冒号(注意 ASP.NET 通过以下划线开头的保留 ID 打破了这个规则——因此它们总是会导致 HTML/XML lint 失败——实际上一些代理剥离它们)。要查找任何 HTML 元素,id
请使用:
document.getElementById(idvalue)
this only returns one DOM node.
这只会返回一个 DOM 节点。
回答by Ishmael
The name
attribute is not unique. For instance, it is used to group radio buttons. It represents the value of a particular form property. id
s must be unique.
该name
属性不是唯一的。例如,它用于对单选按钮进行分组。它表示特定表单属性的值。id
s 必须是唯一的。
回答by Alan Barber
ID should be unique but you can use multiple form elements with the same NAME. This is standard for how radio buttons work so you can force one seletion of a radio button group.
ID 应该是唯一的,但您可以使用多个具有相同 NAME 的表单元素。这是单选按钮如何工作的标准,因此您可以强制选择一个单选按钮组。