Can the select option value be of different types?

前端 未结 4 562
别跟我提以往
别跟我提以往 2020-12-20 11:58

I want to know what is good practice for select option values.

Example


                        
    
提交评论

  • 2020-12-20 12:29

    The posted value will be the one corresponding to the selection.

    In that regards, it is treated the same way as an input type text is.

    0 讨论(0)
  • 2020-12-20 12:32

    Same as a text-type input -- it can be string, float, etc. This is more a question of which is most reliable to parse when you process the form data.

    0 讨论(0)
  • 2020-12-20 12:39

    There are no limits real to the type of data that can be set in the value attribute of the option element. Characters with special meaning in HTML do, of course, need to be represented by the appropriate entities (& as & for example (although the one in the question meets the "followed by a space character" exception to the rule)).

    The attribute is defined as containing CDATA:

    <!ELEMENT OPTION - O (#PCDATA)         -- selectable choice -->
    <!ATTLIST OPTION
      %attrs;                              -- %coreattrs, %i18n, %events --
      selected    (selected)     #IMPLIED
      disabled    (disabled)     #IMPLIED  -- unavailable in this context --
      label       %Text;         #IMPLIED  -- for use in hierarchical menus --
      value       CDATA          #IMPLIED  -- defaults to element content --
      >
    

    — http://www.w3.org/TR/html4/interact/forms.html#h-17.6

    CDATA is a sequence of characters from the document character set and may include character entities. User agents should interpret attribute values as follows:

    • Replace character entities with characters,
    • Ignore line feeds,
    • Replace each carriage return or tab with a single space.

    User agents may ignore leading and trailing white space in CDATA attribute values (e.g., " myval " may be interpreted as "myval"). Authors should not declare attribute values with leading or trailing white space.

    For some HTML 4 attributes with CDATA attribute values, the specification imposes further constraints on the set of legal values for the attribute that may not be expressed by the DTD.

    — http://www.w3.org/TR/html4/types.html#type-cdata

    The specification doesn't impose additional limits for the option element's value attribute.

    0 讨论(0)
  • 提交回复
    热议问题