HTML 5 区别输入 id 和输入名称?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10165908/
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
HTML 5 difference input id and input name?
提问by Nick Zijlstra
I'm busy with something for school in HTML 5.
我正忙于在 HTML 5 中为学校准备的东西。
So here is my bit of code
所以这是我的代码
<label for="name">Name</label>
<input type="text" id="name" name="name" placeholder="your name" required><br>
So my question actually is:
所以我的问题实际上是:
What is the difference between the NAME and the ID? purpose? which one is more important?
NAME 和 ID 之间有什么区别?目的?哪个更重要?
回答by Frambot
In short, the name
is the identifier that is sent to the server when you submit the form. The id
is a unique identifier for the browser, clientside, for javascript and such.
简而言之,name
就是提交表单时发送到服务器的标识符。的id
是用于浏览器中,客户机侧的唯一标识符,用于javascript和这样。
回答by Phrogz
The name
attribute is for submitting a form element to the server; many elements may share the same name
(e.g. radio buttons, which musthave the same name within the set).
该name
属性用于向服务器提交表单元素;许多元素可能共享相同的name
(例如,单选按钮,在集合中必须具有相同的名称)。
The id
attribute is for uniquely identifying any element (not just form elements). It mustbe unique throughout the entire document.
该id
属性用于唯一标识任何元素(不仅仅是表单元素)。它在整个文档中必须是唯一的。
回答by Tony R
The id
attribute is supposed to be unique in your document. Only one element can have a given id. document.getElementById()
finds the first element with the given id
.
该id
属性在您的文档中应该是唯一的。只有一个元素可以有一个给定的 id。document.getElementById()
查找具有给定 的第一个元素id
。
The name
attribute is used by forms as the key in a key/value pair when submitting the form. The value
attribute is both displayed in the browser, and submitted with the form.
name
提交表单时,该属性被表单用作键/值对中的键。该value
属性既显示在浏览器中,又与表单一起提交。
Neither is "more important," they're just different. If you have an XML mindset, they're both just attributes on a node. In HTML they have more meaning though.
两者都不是“更重要”,它们只是不同。如果您有 XML 思维,那么它们都只是节点上的属性。在 HTML 中,它们有更多的含义。