如何在 HTML 选择中添加选项组?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14029080/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 04:33:27  来源:igfitidea点击:

How to add Option group in HTML select?

html

提问by Kashif R

How can I add option group in HTML select tag? I want to categories my option list with option group....how can I use it?

如何在 HTML 选择标签中添加选项组?我想用选项组对我的选项列表进行分类......我该如何使用它?

Here is an example

这是一个例子

<select>
<!-- First category option -->
   <option value="volvo">Volvo</option>
   <option value="saab">Saab</option>
<!-- Second catgory option -->
   <option value="mercedes">Mercedes</option>
   <option value="audi">Audi</option>
</select> 

回答by Kashif R

By using the optgrouptag. Here is an example:

通过使用optgroup标签。下面是一个例子:

<select name="browser">

    <optgroup label="Firefox">
      <option value="2.0 or higher">
        Firefox 2.0 or higher
      </option>
      <option value="1.5.x">Firefox 1.5.x</option>
      <option value="1.0.x">Firefox 1.0.x</option>
    </optgroup>

    <optgroup label="Microsoft Internet Explorer">
      <option value="7.0 or higher">
        Microsoft Internet Explorer 7.0 or higher
      </option>
      <option value="6.x">Microsoft Internet Explorer 6.x</option>
      <option value="5.x">Microsoft Internet Explorer 5.x</option>
      <option value="4.x">Microsoft Internet Explorer 4.x</option>
    </optgroup>

    <optgroup label="Opera">
      <option value="9.0 or higher">Opera 9.0 or higher</option>
      <option value="8.x">Opera 8.x</option>
      <option value="7.x">Opera 7.x</option>
    </optgroup>

    <option>Safari</option>
    <option>Other</option>

</select>

hope it helps.

希望能帮助到你。

回答by Paul S.

You are looking for the <optgroup>tag in which you should nest your options.

您正在寻找<optgroup>应该嵌套选项的标签。

回答by Dejan Janju?evi?

Optgroupis what you are looking for.

Optgroup正是您要找的。