Is this a non-value selection option, valid?

Say I have this choice. If I publish this, the value will be: city 1, city 2, city 3 and city 4. I do not want to give them a value, because the value is the same as the option name.

This is normal?

<select> <option>City 1</option> <option>City 2</option> <option>City 3</option> <option>City 4</option> </select> 
+5
source share
2 answers

If there is no value, your text will be used. You can find this information in the HTML standard:

The value attribute provides the value for the item. The value of the option element is the value of the content value attribute, if any, or, if it is not, the value of the IDL attribute of the element text.
https://html.spec.whatwg.org/multipage/forms.html#the-option-element

or in the W3C Wiki

Provides a value for the item. If this does not happen, the value of the option element is the textContent of the element.
https://www.w3.org/wiki/HTML/Elements/option

Test case

In the following snippet, you can check the behavior of the parameters: http://jsfiddle.net/ghqp08kr/

 <form action="http://posttestserver.com/post.php" method="post"> <select name="values"> <option value="with-value">With Value</option> <option>Without Value</option> <option value="">With Empty Value</option> </select> <input type="submit"/> </form> 
 <!-- With Value --> key: 'values' value: 'with-value' <!-- Without Value --> key: 'values' value: 'Without Value' <!-- Without Empty Value --> key: 'values' value: '' 
+12
source

Although this is acceptable, it is impractical since all browsers and web servers interpret the same thing. It is always better to give such values, even if they are simply repeated:

 <select> <option value="City 1">City 1</option> <option value="City 2">City 2</option> <option value="City 3">City 3</option> <option value="City 4">City 4</option> </select> 

From docs :

value

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

+2
source

Source: https://habr.com/ru/post/1238118/


All Articles