简体   繁体   English

我什么时候应该在HTML4 / HTML5中使用name属性?

[英]When should I use the name attribute in HTML4/HTML5?

I know by reading the W3C documention for HTML4.01 and HTML5 that the "name" attribute originally existed as a property of the <a> tag to permit people to link to an anchor point within a document. 我知道通过阅读HTML3.01和HTML5的W3C文档,“name”属性最初作为<a>标签的属性存在,以允许人们链接到文档中的锚点。

However, now that all major browser venders allow linking to any html element within a document via the "id" attribute, is there still any real use for the "name" attribute? 但是,既然所有主要的浏览器供应商都允许通过“id”属性链接到文档中的任何html元素,那么“name”属性是否还有实际用途? If so, how should I be using the "name" attribute? 如果是这样,我应该如何使用“名称”属性?

One thing that comes to mind are radio buttons: you have to use name to specify which ones are part of the same group. 想到的一件事是单选按钮:您必须使用name来指定哪些是同一组的一部分。

<form>
<input type="radio" name="sex" value="male" /> Male<br />
<input type="radio" name="sex" value="female" /> Female
</form> 

我认为,在input元素(和他们的朋友)上需要name属性...

<input type="text" name="email" value="" />

Good question... As mentioned in other answers one obvious use is for radio buttons so that only one radio button can be selected at a time, as you can see in jQuery radio buttons - choose only one? 好问题......正如其他答案中所提到的,一个明显的用途是radio buttons这样一次只能选择一个单选按钮,正如你在jQuery单选按钮中看到的那样- 只选择一个?

Along with this, in ASP.Net MVC I have found another use of the name attribute. 与此同时,在ASP.Net MVC我发现了name属性的另一种用法。 Refer MVC which submit button has been pressed 请参阅已按下提交按钮的MVC

<input name="submit" type="submit" id="submit" value="Save" />
<input name="submit" type="submit" id="process" value="Process" />

From http://www.w3schools.com/tags/att_button_name.asp 来自http://www.w3schools.com/tags/att_button_name.asp

The name attribute specifies the name for a element. name属性指定元素的名称。

The name attribute is used to reference form-data after the form has been submitted, or to reference the element in a JavaScript. name属性用于在表单提交后引用表单数据,或引用JavaScript中的元素。

Tip: Several elements can share the same name. 提示:多个元素可以共享相同的名称。 This allows you to have several buttons with equal names, which can submit different values when used in a form. 这允许您使用具有相同名称的多个按钮,这些按钮在表单中使用时可以提交不同的值。

Other References 其他参考文献

  1. HTML5 How To Skip Navigation When Name Attribute Is Obsolete HTML5如何在名称属性过时时跳过导航
  2. HTML5 Obsolete features HTML5过时的功能
  3. HTML input - name vs. id HTML输入 - 名称与ID

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

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