是一个没有值的选择选项,有效吗? [英] is a select option with no value, valid?

查看:74
本文介绍了是一个没有值的选择选项,有效吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我选择了此选项.如果我发布此信息,则该值为:城市1,城市2,城市3和城市4.我不想为他们提供值,因为该值与选项名称相同.

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.

这样可以吗?

<select>
    <option>City 1</option>
    <option>City 2</option>
    <option>City 3</option>
    <option>City 4</option>
</select>

推荐答案

如果没有值,将使用您的文本.您可以在HTML标准上找到以下信息:

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

value属性提供元素的值.选项元素的值是值内容属性的值(如果有),或者如果没有则是元素的文本IDL属性的值.
https://html.spec.whatwg.org/multipage/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 IDL attribute.
https://html.spec.whatwg.org/multipage/forms.html#the-option-element

或在W3C Wiki上

or on the W3C Wiki

提供元素的值. 如果没有,则option元素的值是该元素的textContent.
https://www.w3.org/wiki/HTML/Elements/option

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

测试用例

使用以下代码片段,您可以测试选项的行为: http://jsfiddle.net/ghqp08kr/

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>

<!-- With Value -->
key: 'values' value: 'with-value'

<!-- Without Value -->
key: 'values' value: 'Without Value'

<!-- Without Empty Value -->
key: 'values' value: ''

这篇关于是一个没有值的选择选项,有效吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆