简体   繁体   中英

Are Multi-line Options in Html Select Tags Possible?

是否有可能(仅使用HTML),显示selectoption跨越每个多线S?

It is not possible using html select control.

You can use a div that can act as a dropdown list using JavaScript and css.

not only is it not possible on standard html, but it would then (as an object) become incompatible with the way IOS devices handle the option tag , which is to display a scroll list so it is not even reasonable to want the option tag to behave that way as it would break cross-device compatibility badly and unexpectedly.

as others have answered (i dont have enough reputation to upvote or comment yet) have said, it must be done with css/div styling etc and in that way is a lot more extensible with full html functionality within each of the option tag 's as well as (via css styling) being mobile device friendly.

If your case is around iOS truncating long option text, then the solution from How to fix truncated text on <select> element on iOS7 can help.

Add an empty optgroup at the end of the select list:

You can implement like this:

<select>
  <option selected="" disabled="">option first</option>
  <option>another option that is really long and will probably be truncated on a mobile device</option>
  ...
  <optgroup label=""></optgroup>
</select>

As the presentation of a select element is up to the user agent, I'm afraid you can't have that, unless some UA actually implements it. But select as either a ListBox or ComboBox never really had much need for items spanning multiple lines. Furthermore it would greatly confuse users as they are used to one line = one item.

No.

You could use radio buttons instead though, their <label> s can word wrap.

It would be possible by using some JavaScript with CSS styling on HTML elements, easily done with a framework like Dojo Toolkit. Otherwise, use Radio or Checkbox controls.

What about:

<!DOCTYPE html>
<html>
<body>

<select size="13" multiple>
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="opel">Opel</option>
  <option value="audi">Audi</option>
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="opel">Opel</option>
  <option value="audi">Audi</option>
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="opel">Opel</option>
  <option value="audi">Audi</option>
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="opel">Opel</option>
  <option value="audi">Audi</option>
</select>

</body>
</html>

I don't know if this is what you were looking for, but maybe it could help you.

If you want to select multiple options, you must press Ctrl + click to select more options.

If you want to disable multiselect, just erase the "multiple" parameter from the SELECT tag.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM