Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/443.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/89.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用javascript从下拉列表中删除组标题_Javascript_Jquery - Fatal编程技术网

使用javascript从下拉列表中删除组标题

使用javascript从下拉列表中删除组标题,javascript,jquery,Javascript,Jquery,我使用此代码对dropdownlist控件的项目进行分组: var item; var GroupsGRP = jQuery('<optgroup/>', { label: 'Groups' }).appendTo(selectControl); var EmployeesGRP = jQuery('<optgroup/>', { label: 'Employees' }).appendTo(selectControl); jQuery('optio

我使用此代码对dropdownlist控件的项目进行分组:

var item;

var GroupsGRP = jQuery('<optgroup/>', {
    label: 'Groups'
}).appendTo(selectControl);

var EmployeesGRP = jQuery('<optgroup/>', {
    label: 'Employees'
}).appendTo(selectControl);

jQuery('option', selectControl).each(function (i) {
    item = jQuery(this);

    if (item.attr("class") == 'Employees') {
        item.appendTo(EmployeesGRP);
    }
    else {
        item.appendTo(GroupsGRP);
    }

});
问题:


如果没有项目,我如何删除组标题?

只需将选项与要分组的类合并,其余的将保持未分组状态:

假设您的html是:

<SELECT id="MainContent_DropDownList1">
    <option>Select One</option>
    <option class="Employees">Employee number one</option>
    <option class="Employees">Employee number two</option>
    <option class="Groups">Group number one</option>
    <option class="Groups">Group number two</option>
    <option>Not an employee or group</option>
</SELECT>
以及jquery:

$(function() {

    var EmployeesGRP = $('<optgroup/>');
    var GroupsGRP = $('<optgroup/>');

    EmployeesGRP .attr('label', 'Employees');
    GroupsGRP .attr('label', 'Groups');

    $('#MainContent_DropDownList1 .Employees').wrapAll(EmployeesGRP);
    $('#MainContent_DropDownList1 .Groups').wrapAll(GroupsGRP);

});

如果您有选项,那么只有它会创建optgroup,对吗?你能像在小提琴上一样在线显示你的代码吗?我不想打开任何项目,我想实现的是,如果员工没有任何项目,我希望员工标题被删除@davidg如果你看JSFIDLE live示例,从SELECT语句中删除2个Employee选项,您将不会看到为Employee显示的组标题。确实如此!:我没有先检查是多么愚蠢,非常感谢@DavidG
$(function() {

    var EmployeesGRP = $('<optgroup/>');
    var GroupsGRP = $('<optgroup/>');

    EmployeesGRP .attr('label', 'Employees');
    GroupsGRP .attr('label', 'Groups');

    $('#MainContent_DropDownList1 .Employees').wrapAll(EmployeesGRP);
    $('#MainContent_DropDownList1 .Groups').wrapAll(GroupsGRP);

});