2012-12-25 75 views
14

如何在HTML選擇標籤中添加選項組? 我想將選項列表與選項組分類....我如何使用它?如何在HTML選擇中添加選項組?

下面是一個例子

<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> 

回答

35

通過使用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> 

希望它有幫助。

+1

我想你的意思是「價值」屬性,而不是「標籤」屬性中的

+1

@ g.annunziata不符合Mozilla:https://developer.mozilla.org/en/docs/Web/HTML/Element/optgroup – Brett

4

您正在查找需要嵌套選項的<optgroup>標籤。

3

Optgroup是你在找什麼。

+1

鏈接到w3scools不是一個好主意,最好使用MDN:https:// developer.mozilla.org/en/docs/Web/HTML/Element/optgroup – Brett

相關問題