admin管理员组文章数量:1405918
I have the following categories displayed in a select box:
<form>
<select class="favoritefood">
<optgroup label="Dairy products">
<option>Cheese</option>
<option>Egg</option>
</optgroup>
<optgroup label="Vegetables">
<option>Cabbage</option>
<option>Lettuce</option>
<option>Beans</option>
<option>Onions</option>
<option>Courgettes</option>
</optgroup>
</select>
</form>
Is there any way to go another level down, ie, a subcategory? I've tried using optgroup inside optgroup but it doesn't work.
I have the following categories displayed in a select box:
<form>
<select class="favoritefood">
<optgroup label="Dairy products">
<option>Cheese</option>
<option>Egg</option>
</optgroup>
<optgroup label="Vegetables">
<option>Cabbage</option>
<option>Lettuce</option>
<option>Beans</option>
<option>Onions</option>
<option>Courgettes</option>
</optgroup>
</select>
</form>
Is there any way to go another level down, ie, a subcategory? I've tried using optgroup inside optgroup but it doesn't work.
Share Improve this question asked Feb 26, 2014 at 22:09 user882670user882670 1- 2 <select> is pretty bare-bones. You may want to consider a List-based apprach, such as the one used in Bootstrap. getbootstrap./ponents/#dropdowns – Diodeus - James MacFarlane Commented Feb 26, 2014 at 22:13
3 Answers
Reset to default 2Or just put one or more spaces -   - before your text in the option.
You should create a custom dropdown for this purpose. Here are the steps:
Hide the original dropdown with CSS (display:none;) OR create a hidden field that will contain the value selected by the user
<input type="hidden" name="selection" id="selection">
Create a unordered list (ul) with as many nested li and sub ul
<ul class="custom-drop"> <li class="option-group"> <ul> <li class="heading">Vegetables</li> <li>Cabbage</li> </ul> </li> <li class="option-group"> <ul> <li class="heading">Dairy products</li> <li>Cheese</li> </ul> </li> </ul>
Style this newly created ul as per your needs
- Listen for the
click
event on li inside this ul and set the appropriate option inside the hidden dropdown OR set the value of hidden field so that it can be sent with the form.
So if you are using jQuery then do something like this:
$('body').on('click', '.custom-drop li', function() {
$('#selection').val($(this).text());
// Probably you want to handle showing/hiding the custom drop here
});
You cant go any deeper. It is not allowed for the optgroup. Read this: http://www.w3/TR/html401/interact/forms.html#h-17.6
本文标签: javascriptDisplay subcategories in HTML select boxStack Overflow
版权声明:本文标题:javascript - Display subcategories in HTML select box - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744939366a2633389.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论