Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
549 views
in Technique[技术] by (71.8m points)

html - is a select option with no value, valid?

Let's say that I have this select. If I post this, the value would be: city 1, city 2, city 3 and city 4. I don't want to give them a value, because the value is the same as the option-name.

Is this okay?

<select>
    <option>City 1</option>
    <option>City 2</option>
    <option>City 3</option>
    <option>City 4</option>
</select>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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 IDL attribute.
https://html.spec.whatwg.org/multipage/forms.html#the-option-element

or on the W3C Wiki

Provides a value for element. If there isn't, the value of an option element is the textContent of the element.
https://www.w3.org/wiki/HTML/Elements/option

Test Case

With the following snippet you can test the behaviour of the options: 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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...