我刚刚开始学习如何使用 jquery,并且我有这段代码应该对我的下拉项目进行分组,无论它是“员工”还是“组”,但输出不是我想要的。请帮忙。提前致谢
我的代码:
var selectControl = document.getElementById('MainContent_DropDownList1');
var item;
var EmployeesGRP = jQuery('<optgroup/>');
var GroupsGRP = jQuery('<optgroup/>');
EmployeesGRP .attr('label', 'Employees');
GroupsGRP .attr('label', 'Groups');
jQuery('option', selectControl).each(function (i) {
item = jQuery(this);
if (item.attr("class") == 'Employees') {
item.wrapAll(EmployeesGRP );
}
else {
item.wrapAll(GroupsGRP);
}
});
结果是:
Employees
Employee number one
Employees
Employee number two
Groups
Group number one
Groups
Group number two
代替:
Employees
Employee number one
Employee number two
Groups
Group number one
Group number two