简体   繁体   English

单击另一个select2下拉菜单时如何保持select2下拉事件打开

[英]How to keep select2 dropdown event open when clicking on another select2 dropdown

I have multiple select2 dropdowns. 我有多个select2下拉菜单。 Whenever the select2 dropdown is open, I want to add class to my div. 每当select2下拉列表打开时,我都想将类添加到div中。

For this I am using open and close event of select2. 为此,我正在使用select2的打开和关闭事件。 The below code works fine when you open a select2 dropdown and close it. 当您打开并关闭select2下拉列表时,以下代码可以正常工作。 But it does not work if you have a select2 dropdown open and click on another select2 dropdown. 但是,如果您打开了一个select2下拉菜单并单击另一个select2下拉菜单,则此方法不起作用。 In that case it detects close event. 在这种情况下,它将检测到关闭事件。

$('select').on('select2:open', function (e) {
    $('.a').addClass("header-index");
});
$('select').on('select2:close', function (e) {
    $('.a').removeClass("header-index");
});

How do I make sure that when select2 dropdown is open and user tries to open another select2 dropdown then close event is not fired. 我如何确保当select2下拉列表处于打开状态并且用户尝试打开另一个select2下拉列表时,则不会触发close事件。

Try binding elements with IDs 尝试使用ID绑定元素

$('#select1').on('select2:open', function (e) {
    $('.a').addClass("header-index");
});
$('#select1').on('select2:close', function (e) {
    $('.a').removeClass("header-index");
});

or bind elements with index 或用索引绑定元素

$('select')[0].on('select2:open', function (e) {
    $('.a').addClass("header-index");
});
$('select')[1].on('select2:close', function (e) {
    $('.a').removeClass("header-index");
});

Try this way 试试这个

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

$('.js-example-events').on("select2:open", function () { alert(1); });
$('.js-example-events').on("select2:close", function () { alert(2); });

<select class="js-example-events">
<option>Select</option>
<option>Select1</option>
</select>

https://jsfiddle.net/lalji1051/4qucL9h3/7/ https://jsfiddle.net/lalji1051/4qucL9h3/7/

You can use filter() and select2('isOpen') 您可以使用filter()select2('isOpen')

 $('select').select2() $(document).on('select2:open select2:close', function(e) { let opened = $('select').filter(function() { return $(this).select2('isOpen') }).length $('.a').toggleClass('header-index', Boolean(opened)); }); 
 .header-index { color: red; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script> <link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.2/css/select2.min.css" rel="stylesheet" /> <script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.2/js/select2.min.js"></script> <div class="a">header-index</div> <select style="width: 300px"> <option value="JAN">January</option> <option value="FEB">February</option> <option value="MAR">March</option> <option value="APR">April</option> <option value="MAY">May</option> <option value="JUN">June</option> <option value="JUL">July</option> <option value="AUG">August</option> <option value="SEP">September</option> <option value="OCT">October</option> <option value="NOV">November</option> <option value="DEC">December</option> </select> <select style="width: 300px"> <option value="JAN">January</option> <option value="FEB">February</option> <option value="MAR">March</option> <option value="APR">April</option> <option value="MAY">May</option> <option value="JUN">June</option> <option value="JUL">July</option> <option value="AUG">August</option> <option value="SEP">September</option> <option value="OCT">October</option> <option value="NOV">November</option> <option value="DEC">December</option> </select> 

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

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