简体   繁体   中英

how to detect if select2 has been loaded upon clicking the input field in jquery?

Hi is there a way to check if the select2 options has been loaded upon clicking the input field ? because what happening now is, when I click the input box, nothing happen even if the box got loaded, i had to click it the second time before something happens like eg

$('form#myform .selection').click(function(){
     //how to detect if select2 option are loaded ?
});

You can check for the length property of the option selector for select2, you can use it the same way within the click event :

 $(document).ready(function(){ console.log(" KAD : " + $("#kad").find('option').length); console.log(" KAD2 : " + $("#kad2").find('option').length); if($("#kad").find('option').length > 0) { // has options } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <select id="kad"></select> <select id="kad2"> <option > 1 </option><option > a </option><option > b </option> </select> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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