如何使用jquery选择select2 selectbox选项?
我尝试用jquery选择我的选择框的如何使用jquery选择select2 selectbox选项?,jquery,select2,Jquery,Select2,我尝试用jquery选择我的选择框的bird。但无论我做什么,它始终是cat: $(函数(){ $(“.select2”).select2(); $(“.select2”).val('bird'); $('.select2').find('option[value=“bird”]').prop('selected',true); }); 猫 狗 鸟 您需要在如下设置值后触发更改事件: $(函数(){ $(“.select2”).select2(); $(“.select2”).val('b
bird
。但无论我做什么,它始终是cat
:
$(函数(){
$(“.select2”).select2();
$(“.select2”).val('bird');
$('.select2').find('option[value=“bird”]').prop('selected',true);
});代码>
猫
狗
鸟
您需要在如下设置值后触发更改
事件:
$(函数(){
$(“.select2”).select2();
$(“.select2”).val('bird').trigger(“change”);
});代码>
猫
狗
鸟
$(函数(){
$(“.select2”).select2();
$(“.select2”).val('bird');
$('.select2').find('option[value=“bird”]').prop('selected',true.change();
});代码>
猫
狗
鸟
Select2如果添加到选项中,则会尊重您的初始选定属性。我不确定这是否适用于您的案例,但它可能是最具语义的解决方案
$(函数(){
$(“.select2”).select2();
});代码>
猫
狗
鸟
try$('.select2').find('option[value=“bird”]').prop('selected',true)。change()