jQuery事件帮助 - 我有一个选择列表OptGroups,我想只在选项属于某个optgroup时调用函数

问题描述:

我有一个选择列表与optgroups。我想为下拉选定的索引更改添加处理程序,我如何知道所选项目属于哪个optgroup?这决定了更多的执行路径。我明白如何添加功能,功能内容更多是问题。jQuery事件帮助 - 我有一个选择列表OptGroups,我想只在选项属于某个optgroup时调用函数

$ddl.bind("change", function(){ 
    //how do I find out which option group the selected option belongs to? 
    var selectList = $(this); 
}); 

感谢您的任何提示。

干杯, 〜CK

+0

我想通了这一点。我使用selectbox jQuery插件。 var open =“Open”; var tmpInner = $(this).selectedValues()[0]; var group = $(this).selectedOptions()。parent(); (group。[0] .label == open){ $(“。addressDDL”)。selectOptions(tmpInner).change(); } – Hcabnettek 2010-01-07 19:41:24

<!DOCTYPE html> 
<html> 
<head> 
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script> 
    <title>test</title> 
</head> 
<body> 
<select id="marcas"> 
    <optgroup label="ford"> 
     <option>ka</option> 
     <option>fiesta</option> 
     <option>mondeo</option> 
    </optgroup> 
    <optgroup label="peugeot"> 
     <option>305</option> 
     <option>306</option> 
     <option>205</option> 
    </optgroup> 
</select> 
</body> 
</html> 
<script> 
$(function(){ 

    $("#marcas").change(function() { 
     alert($(this).find(":selected").parent().attr("label")); 
    }); 

}); 
</script> 
+0

感谢您的提示Andres! – Hcabnettek 2010-01-10 21:47:18