通过jQuery关闭任何Select2下拉菜单 [英] Close ANY Select2 dropdowns via jQuery
问题描述
有人知道如何以编程方式不知道ID的情况下关闭Select2吗??基本上,如果有Select2打开,则将其关闭.
(如果有人打开模态(包含Select2),打开下拉列表,然后单击远离/放弃该模态,则模态关闭,但Select2继续显示,直到再次单击为止.)
例如,这有效但是您需要知道ID :
// If a Modal is hidden, close the Select2 contained therein
$(document).on('hide.bs.modal', '.modal', function() {
$("#myDropdown").select2("close");
});
这(按班级选择)不起作用-至少对我来说:
$(document).on('hide.bs.modal', '.modal', function() {
$(".select2").select2("close");
});
我知道还有其他类似的话题,但是我已经审查了很多,但没有找到解决方案.
诸如此类:通过javascript/jquery关闭select2下拉 >
有什么想法吗?
好的,我明白了.将唯一的(不是class ="select2")类添加到模态中所有的Select2下拉菜单中,然后使用以下代码:
// If a Modal is hidden, close any open Select2 contained therein - so it doesn't lag behind
$(document).on('hide.bs.modal', '.modal', function() {
$(".select2-close").select2("close");
});
这似乎很好. (在这种情况下,我制作了一个名为 select2-close 的类)
Anyone know how to close a Select2 programatically without knowing the ID? Just basically, if there's a Select2 open, close it.
(If someone opens a modal (containing a Select2), opens the dropdown, and then clicks away from / dimisses the modal, the modal closes but the Select2 continues to show until another click.)
For instance, this works BUT you need to know the ID:
// If a Modal is hidden, close the Select2 contained therein
$(document).on('hide.bs.modal', '.modal', function() {
$("#myDropdown").select2("close");
});
And this (selecting by class) doesn't work - at least for me:
$(document).on('hide.bs.modal', '.modal', function() {
$(".select2").select2("close");
});
I know there are other similar topics, but I've reviewed a bunch and am not finding a solution.
Such as this: close select2 dropdown via javascript/jquery
Any ideas?
Okay, I got this figured out. Add a unique (not class="select2") class to all the Select2 dropdowns you have in modals, and then use this code:
// If a Modal is hidden, close any open Select2 contained therein - so it doesn't lag behind
$(document).on('hide.bs.modal', '.modal', function() {
$(".select2-close").select2("close");
});
That seems to work well. (In this case I made a class called select2-close )
这篇关于通过jQuery关闭任何Select2下拉菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!