繁体   English   中英

动态将select2类添加到当前单击的元素

[英]add select2 class dynamically to current clicked element

我向当前单击的元素动态添加了select2类,但第二次单击后出现了搜索框。 它应该出现在元素上的第一次单击上。

例如:

$(document).on('click', 'select', function () {             
    $(this).select2();
});

谢谢

在select2初始化之后,我们可以使用命令.select2('open')再次调用它。

希望这是您想要的。

 $(document).on('click', 'select', function() { var select2_open; $(this).select2().select2('open'); }); 
 <link href="https://select2.github.io/dist/css/select2.min.css" rel="stylesheet" /> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://select2.github.io/dist/js/select2.full.js"></script> <select class="select"> <option value="AL">Alabama</option> <option value="WY">Wyoming</option> </select> 

 $('.select').select2(); $(document).on('click', 'select', function() { $(this).select2(); }); 
 <link href="https://select2.github.io/dist/css/select2.min.css" rel="stylesheet" /> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://select2.github.io/dist/js/select2.full.js"></script> <select class="select"> <option value="AL">Alabama</option> <option value="WY">Wyoming</option> </select> 

您不需要添加onclick

只是$('select').select2();

https://jsfiddle.net/vandolphreyes29/z5ogefz8/7/

暂无
暂无

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

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