I have a select list with optgroups. I want to add a handler for the drop down selected index change, how d开发者_运维知识库o I tell which optgroup the selected item belongs to? This determines further execution path. I understand how to add the function, the function contents are more the issue.
$ddl.bind("change", function(){
//how do I find out which option group the selected option belongs to?
var selectList = $(this);
});
Thanks for any tips.
Cheers, ~ck
<!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>
精彩评论