简体   繁体   English

html名称属性的目的是什么?

[英]What is the purpose of the html name attribute?

What is the purpose of the name attribute on input , span , or div tags? inputspandiv标签上name属性的用途是什么? Should an id tag be used instead? 应该使用id标签吗?

Example from Mozilla's documentation : Mozilla文档中的示例:

<label for="User">Click me</label>
<input type="text" id="User" name="Name" />

The name attribute identifies the input value if it is sent to a server via a traditional GET or POST of a form. 如果通过表单的传统GETPOSTname属性发送到服务器,则name属性标识输入value

Specific to the example if you had: 具体到示例,如果你有:

<form action="http://localhost" method="POST">
    <label for="User">Click me</label>
    <input type="text" id="User" name="Name" value="foo" />
    <input type="submit" value="submit" />
</form>

and you submit the form, the server localhost will receive a content body like: 并且您提交表单,服务器localhost将收到如下内容正文:

Name=foo

which as another post mentions, is usually parsed by a server side language like PHP into something easier to work with. 正如另一篇文章所提到的,它通常由像PHP这样的服务器端语言解析成更易于使用的东西。

The id attribute identifies the input to the DOM . id属性标识DOM的输入。 If you specify an input with no name but an id and try to submit it via a GET or POST it will not be parsed correctly by the server. 如果您指定的输入没有name而是id并尝试通过GETPOST提交,则服务器将无法正确解析。

In an input element, the name attribute defines the name of the control. input元素中, name属性定义控件的名称。 Only controls that have a name can be “successful”, ie may contribute to the form data set sent to a server. 只有具有名称的控件才能“成功”,即可能有助于发送到服务器的表单数据集。 Thus, it is indispensable in order to make the value of the control submitted to server-side processing. 因此,为了使控制的值提交给服务器端处理,它是必不可少的。 The id attribute has nothing to do with this; id属性与此无关; it has its own uses, such as helping to associate a label with the control, as in the example. 它有自己的用途,例如帮助将标签与控件相关联,如示例所示。

Consequently, the name attribute is not needed if you need not have the control value submitted. 因此,如果您不需要提交控件值,则不需要name属性。 For example, if you have a single submit button like <input type=submit value=Send> , you don't need it; 例如,如果您有一个像<input type=submit value=Send>这样的提交按钮,则不需要它; but if you have several submit buttons and you need to recognize, server-side, which one was used, you need it. 但如果你有几个提交按钮,你需要识别,服务器端,使用哪一个,你需要它。 If the form data is not sent to server-side processing but handled only in client-side processing, the name attribute is not used, as you can access the values with other means. 如果表单数据未发送到服务器端处理但仅在客户端处理中处理,则不使用name属性,因为您可以使用其他方法访问值。

In span and div elements, a name attribute is invalid in all HTML versions. spandiv元素中, name属性在所有HTML版本中都无效。 If used, it will simply be ignored, except in the sense that it is stored (like any invalid attributes) in the attributes property in the DOM and can be accessed in client-side scripting. 如果使用它,它将被简单地忽略,除非它在DOM的attributes属性中存储(如任何无效属性),并且可以在客户端脚本中访问。

Considering the general question as in the question title, the name attribute is allowed and recognized in some elements, disallowed and ignored in others, and when allowed, its meaning is defined in the definition of the element. 考虑到问题标题中的一般问题, name属性在某些元素中被允许和识别,在其他元素中被禁止和忽略,并且在允许时,其含义在元素的定义中定义。 There is not that much in common between these definitions. 这些定义之间没有太多共同之处。

Some confusion has been caused by statements like “the name attribute is deprecated/obsolete”. 一些混淆是由“ name属性已弃用/过时”之类的语句引起的。 Such statements are correct for this attribute in some elements in some HTML versions, but in input elements for example, it remains official and necessary. 这些语句在某些HTML版本的某些元素中对于此属性是正确的,但在input元素中,例如,它仍然是官方和必要的。

Name should be used on form fields like <input> , it should not be used on <span> or <div> . 名称应该用在<input>类的表单字段上,不应该在<span><div> Name is obsolete on anchor tags, and you should use id instead. 名称在锚标记上已过时,您应该使用id。

Check this mozilla link for more: 检查此mozilla链接以获取更多信息:

name gets or sets the name property of a DOM object; name获取或设置DOM对象的name属性; it only applies to the following elements: <a> , <applet> , <button> , <form> , <frame> , <iframe> , <img> , <input> , <map> , <meta> , <object> , <param> , <select> , and <textarea> 它仅适用于以下元素: <a><applet><button><form><frame><iframe><img><input><map><meta><object><param><select><textarea>

If you define a link (a-Element) and define a name for it, you can use it as an anchor. 如果定义链接(a-Element)并为其定义名称,则可以将其用作锚点。 See: http://www.w3schools.com/tags/att_a_name.asp 请参阅: http//www.w3schools.com/tags/att_a_name.asp

or more generally: https://developer.mozilla.org/en-US/docs/Web/API/Element.name 或更一般地说: https//developer.mozilla.org/en-US/docs/Web/API/Element.name

The name attribute is for when you are using an additional language such as PHP to read and write data to the database or other files. name属性适用于使用PHP等其他语言读取数据或将数据写入数据库或其他文件的情况。 To refer to the text field above, you would refer to its name which is "Name" and write the value in the "Name" text field to the database. 要引用上面的文本字段,您可以引用其名称“Name”,并将“Name”文本字段中的值写入数据库。

Hope this helps clarify things.. 希望这有助于澄清事情..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM