名称属性在 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 07:44:49  来源:igfitidea点击:

Does a name attribute have to be unique in a HTML document?

htmlunique

提问by Vilx-

I remember reading in the spec once that both the idattribute and the nameattribute 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 idand nameto the same element.

我记得有一次在规范中读到id属性和name属性共享相同的命名空间并且必须是唯一的。从今以后,我一直试图在我的应用程序中满足这个要求,甚至害怕给相同的元素idname相同的元素。

But lately I've started working with ASP.NET MVC 3, and it (like PHP) can use the same nameattribute 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 nameattribute is only valid on the <form>and form elements (<input>,<textarea>and <select>). It's used to specify the nameto 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.elementscollection by specifying the nameas the index. If nameis 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.

注意:即使只找到一个元素,它也总是返回一个数组。

idattribute is from the XML world, and is a unique id for any node, not just form elements. Unlike the nameattribute it is valid on any HTML node. Also like the nameattribute, 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 idyou 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 nameattribute is not unique. For instance, it is used to group radio buttons. It represents the value of a particular form property. ids must be unique.

name属性不是唯一的。例如,它用于对单选按钮进行分组。它表示特定表单属性的值。ids 必须是唯一的。

回答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 的表单元素。这是单选按钮如何工作的标准,因此您可以强制选择一个单选按钮组。