开发者

Are there benefits to specifying OPTION elements with mirrored value attributes?

开发者 https://www.devze.com 2023-02-14 23:07 出处:网络
I\'m wondering, is there a productive reason for doing this: <option value=\"My Value\">My Value</option>

I'm wondering, is there a productive reason for doing this:

<option value="My Value">My Value</option>

As opposed to this:

<option>My Value</option>

In other words, specifically specifying a value attribute, even when the value it contains is not different from the content within the option tag?

I often see the first in professionally developed software packages. Just wondering what benefits or background there is to doing the former when it doesn't appear necessary, or if it doesn开发者_如何转开发't matter (in other words, coding style/preference).

Note, I realize there are times when this is useful, such as:

<option value="USA">United States</option>

It's when they are the same that I'm wondering what the appropriate thing to do is.

Also, as Kyle points out, the specs say that the second form is entirely legal. So my thought is there might be other reasons for the value being there.


Even in the case where the processing code expects the same value as the displayed option, it's often a good idea to keep the displayed text decoupled from the option's value.

For example, if you ever want to internationalize this site, you're probably going to want to be able to translate the text that is displayed, but not the value attribute.


Often you will see value be a representation of a surrogate key (usually an identity column) in a database, therefore value will usually represent an int or long. In this manner you can display the user friendly value "My Value" while posting the key value in the form post - for example "100".

One could make the argument, however, that you should always provide a "value" attribute as it is more descriptive and easier to script against in the future.


http://www.w3.org/TR/html4/interact/forms.html#adef-value-OPTION

The default value of an option element is the elements content per the w3c.

I'll say this is a coding style thing.

0

精彩评论

暂无评论...
验证码 换一张
取 消