gpt4 book ai didi

Jquery – 在 select 中选择 optgroup

转载 作者:行者123 更新时间:2023-12-03 22:23:33 25 4
gpt4 key购买 nike

我有一个包含 2 个 optgroup 的选择。有没有办法仅在选择第一个 optgroup 中的选项时调用函数,而在选择第二个 optgroup 中的选项时调用另一个函数?

最佳答案

当然。

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/

关于Jquery – 在 select 中选择 optgroup,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5475003/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com