简体   繁体   English

Select2 在第一个 select2 打开事件上打开另一个 select2

[英]Select2 open another select2 on 1st select2 open event

 $(".js-example-events").select2(); $('.js-example-events').on("select2:open", function () { $("#s1").select2("open"); }); function test(){ $("#s1").select2("open"); }
 <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.8/js/select2.min.js"></script> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.8/css/select2.min.css"/> <select class="js-example-events" style="width:300px;"> <option>Select One</option> <option>Select1</option> </select> <select class="js-example-events" id="s1" style="width:300px;"> <option>Selects Two</option> <option>Select1</option> </select> <button onclick="test()"> Test </button>

I want to open 2 select2 at a time.我想一次打开 2 个 select2。

It works when I use a button click event, but not when I use select2 open event它在我使用按钮单击事件时有效,但在我使用 select2 打开事件时无效

<select class="js-example-events" style="width:300px;">
    <option>Select One</option>
    <option>Select1</option>
</select>

<select class="js-example-events" id="s1" style="width:300px;">
    <option>Selects Two</option>
    <option>Select1</option>
</select>

<button onclick="test()">Test</button>

JS JS

$(".js-example-events").select2();

$('.js-example-events').on("select2:open", function () {
    $("#s1").select2("open");  // not working here
});

function test(){
    $("#s1").select2("open"); // but work here well
}

Working JSFiddle工作 JSFiddle

https://jsfiddle.net/gqo5cL6x/ https://jsfiddle.net/gqo5cL6x/

select2("open"); select2("打开"); is not working不管用

you can use tag + class in function.您可以在函数中使用标签 + 类。 ex: $("select[class='js-example-events']").select2("open").例如:$("select[class='js-example-events']").select2("open")。

$("select[class='js-example-events']").on("select2:open", function () { 
   $("#s1").select2("open"); // not working here })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM