jQuery –在选择中选择optgroup [英] Jquery – Select optgroup in select

查看:83
本文介绍了jQuery –在选择中选择optgroup的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有2个optgroup的选择.有一个方法调用仅当从第一OPTGROUP一个选项被选择的功能以及如果选择来自所述第二OPTGROUP一个选项调用另一个函数?

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 second optgroup is selected?

推荐答案

可以.

HTML:

What is your preferred vacation spot?<br>
<SELECT ID="my_select"> 

<OPTGROUP LABEL="OptGroup One." id="one">        
<OPTION LABEL="Florida">Florida</OPTION>         
<OPTION LABEL="Hawaii">Hawaii</OPTION>         
<OPTION LABEL="Jersey">Jersey Shore</OPTION>    
</OPTGROUP> 

<OPTGROUP LABEL="OptGroup Two" id="two">  
<OPTION LABEL="Paris">Paris</OPTION>  
<OPTION LABEL="London">London</OPTION> 
<OPTION LABEL="Florence">Florence</OPTION>  
</OPTGROUP>

</SELECT>

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
    }
});

此处的示例: http://jsfiddle.net/pyG2v/

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

这篇关于jQuery –在选择中选择optgroup的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆