Jquery – Select optgroup in select

后端 未结 3 1217
忘了有多久
忘了有多久 2021-01-12 01:00

I have a select with 2 optgroups. Is there a way to call an function only if an option from the first optgroup is selected and call another function if an option from the se

3条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-12 02:03

    Sure.

    HTML:

    What is your preferred vacation spot?

    JS:

    $("#my_select").change(function(){
        var selected = $("option:selected", this);
        if(selected.parent()[0].id == "one"){
            //OptGroup 1, do something here..
        } else if(selected.parent()[0].id == "two"){
            //OptGroup 2, do something here
        }
    });
    

    Example here: http://jsfiddle.net/pyG2v/

提交回复
热议问题