Is a value attribute required for a HTML SELECT element?

给你一囗甜甜゛ 提交于 2019-12-12 11:32:58

问题


The select menu value posted to the server should be the same as the text in the OPTION element.

On the browser I am using, the following produce identical results.

Is the second script "correct" and cross browser supported?

 <select>
  <option value="AAA">AAA</option>
  <option value="BBB">BBB</option>
  <option value="CCC">CCC</option>
</select>

 <select>
  <option>AAA</option>
  <option>BBB</option>
  <option>CCC</option>
</select>

回答1:


According to MDN the attribute is optional as the value is inferred from the text if it is omitted

The content of this attribute represents the value to be submitted with the form, should this option be selected. If this attribute is omitted, the value is taken from the text content of the option element.




回答2:


Yes, you can omit the value attribute.

Taken from the w3 Standards for <option> tags:

The tag can be used without any attributes, but you usually need the value attribute, which indicates what is sent to the server.

A simple Google search goes a long way.




回答3:


The value attribute is optional according to the HTML 4.01 and 5 specs.

https://www.w3.org/TR/html401/interact/forms.html#h-17.6

value = This attribute specifies the initial value of the control. If this attribute is not set, the initial value is set to the contents of the OPTION element.

https://www.w3.org/TR/html5/forms.html#the-option-element

The value attribute provides a value for element. The value of an option element is the value of the value content attribute, if there is one, or, if there is not, the value of the element's text [...] attribute.




回答4:


The short answer is no. The value attribute is not required but definitely beneficial. Typically just another way to capture the option selected from JS.



来源:https://stackoverflow.com/questions/38854683/is-a-value-attribute-required-for-a-html-select-element

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!