简体   繁体   English

是否有可能通过CSS将选择下拉列表的选项显示为optgroup

[英]Is it possible to make an option of a select dropdown appear as optgroup via css

I am actually populating options from xml for my dropdown and Now adding an optgroup to select is a challenge. 我实际上是从xml的下拉列表中填充选项,现在添加一个optgroup进行选择是一个挑战。 Can I add them manually and change the behaviour via css 我可以手动添加它们并通过CSS更改行为吗

You can add options and optgroups to the select box manually after the fact with jQuery. 您可以在使用jQuery后将选项和optgroups手动添加到选择框。 Assuming your HTML is already available, you could do something like this: 假设您的HTML已经可用,则可以执行以下操作:

$("select").append("<optgroup label='Example'><option>Test1 </option> <option>Test 2</option></optgroup>")

If you already have options in the select element, then it would just be a matter of finding all of those options that you would like to group up (via a class name or value attribute, perhaps), then pushing them into a newly create optgroup, then appending the optgroup into the select. 如果您在select元素中已经有选项,那么只需查找要分组的所有这些选项(也许通过类名或值属性),然后将其推入新创建的optgroup中即可。 ,然后将optgroup附加到select中。 Example: 例:

var optionsToGroup = $("option.groupthis");
var optGroup = $("<optgroup></optgroup>").append(optionsToGroup);
$('select').append(optGroup);

Edit: Based on the Fiddle you've provided, I modified your jQuery code to build the optgroup before the options. 编辑:根据您提供的小提琴,我修改了jQuery代码以在选项之前构建optgroup。 This isn't the most efficient way, but it should get you started based on what you've provided. 这不是最有效的方法,但它应该使您根据所提供的内容开始使用。 See http://jsfiddle.net/xUJZj 看到http://jsfiddle.net/xUJZj

var title = $(this).find('title').text();
var optgrouping = "<optgroup label='"+title+"'></optgroup>";
var options = [];
$(this).find('value').each(function(){
          var value = $(this).text();
          options.push("<option class='ddindent' value='"+ value +"'>"+value+"</option>");
 });
  var grouping = $(optgroupting).html(options.join(''));
  select.append(grouping);

Edit 2: I've modified the JSFiddle to use an actual XML doc (similar to what you provided). 编辑2:我已经修改了JSFiddle以使用实际的XML文档(类似于您提供的文档)。 See it here: http://jsfiddle.net/xUJZj/13/ Or, the relevant modified code is here below: 在此处查看: http : //jsfiddle.net/xUJZj/13/或,下面是相关的修改代码:

function createSelect(xml)
{
                var select = $('#mySelect');
                $(xml).find('menuitem').each(function(){
                    var title = $(this).find('title').text();
                    var optgrouping = "<optgroup label='"+title+"'></optgroup>";
                    var options = [];
                    $(this).find('value').each(function(){
                        var value = $(this).text();
                        options.push("<option class='ddindent' value='"+ value +"'>"+value+"</option>");
                    });
                    var group = $(optgroupting).html(options.join(''));
                    select.append(group);
                });
                select.children(":first").text("please make a selection").prop("selected",true);
            }
        });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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