开发者

jQuery event help - I have a Select list with OptGroups, I want to call function only if option belongs to a certain optgroup

开发者 https://www.devze.com 2022-12-16 05:07 出处:网络
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 furthe

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>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号