我有以下代码:
$(document).ready(function () {
$("#comboSubsidiary").combobox({
selected: function (event, ui) {
var subsidiary = $("#comboSubsidiary").select().val();
switch (subsidiary) {
case (subsidiary = "GH"):
$.ajax({
type: "GET",
url: "GHworkerType.xml",
dataType: "xml",
success: function (xml) {
var select = $('#comboWorkerType');
$(xml).find('type').each(function () {
var type = $(this).find('type').text();
select.append("" + type + "");
var id = $(this).attr('id');
var name = $(this).find('name').text();
var rate = $(this).find('rate').text();
if (name == "Cust" || name == "Int") {
$('<optgroup label="' + name + '"></optgroup>').html(name).appendTo('#comboWorkerType');
}
else {
$('<option>' + name + '</option>').html(name).appendTo('#comboWorkerType');
}
});
select.children(":first").text("Select worker type").attr("selected", true);
}
});
break;
case (subsidiary = "GT"):
alert('Alert');
break;
}
}
});
});
HTML:
<div class="comboTravel">
<select id="comboSubsidiary" >
<optgroup label="Cust">
<option>Select subsidiary</option>
</optgroup>
</select>
</div>
这是 XML:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<worker>
<type>
<name>Cust</name>
<rate>Cust</rate>
</type>
<type>
<name>1</name>
<rate>75</rate>
</type>
<type>
<name>2</name>
<rate>83</rate>
</type>
<type>
<name>3</name>
<rate>105</rate>
</type>
<type>
<name>4</name>
<rate>115</rate>
</type>
<type>
<name>5</name>
<rate>0</rate>
</type>
<type>
<name>Int</name>
<rate>Int</rate>
</type>
<type>
<name>1</name>
<rate>75</rate>
</type>
<type>
<name>2</name>
<rate>83</rate>
</type>
<type>
<name>3</name>
<rate>105</rate>
</type>
<type>
<name>4</name>
<rate>115</rate>
</type>
<type>
<name>5</name>
<rate>0</rate>
</type>
</worker>
我希望组合框将 Cust 和 Int 显示为 optgroup。我怎样才能让它工作?
目前,所有 xml 节点都正确显示在组合框中,但两个选项组未显示,并且选项组中包含的所有内容均未显示在组合框中。