我正在尝试在选择控件中查找当前所选选项的 optgroup 标签的值。下面是一些 html 来显示我想要做什么。
<select id='sector_select' name='sector_select' data-placeholder="Select Sector..." style="width:200px;" class="chzn-select">
<option value='' selected='selected'>All Sectors</a>
<optgroup label="Consultancy Services">
<option value='Employment placement/ recruitment'>Employment placement/ recruitment</option>
</optgroup>
<optgroup label="Supplies">
<option value='Food, beverages and related products'>Food, beverages and related products</option>
</optgroup>
</select>
<script type="text/javascript">
$('#sector_select').change(function ()
{
var label=$('sector_select :selected').parent().attr('label');
console.log(label);
});
</script>
上面的代码给出了 undefined ,因为它读取了 select 元素而不是 option 的父级。有任何想法吗?