简体   繁体   中英

Select 2 onChange() trigger selected

I am using select2 for select box which gets populated on API call. I want to call another API when I select an item from the dropdown list which now works like this:

<select id="person-select" onchange="callAnotherApi()" style="width: 100%">
</select>

API gets called whenever I pick some other value, but not when I pick the first one (which is already selected by default, so no onChange() is triggered). onClick() doesn't function here. I am wondering if I can disable having the first record selected or something to make this work?

You can disable first option and put placeholder text:

 <select>
    <option val="null" disabled selected> select an answer</option>
    <option val="one">one</option>
</select>

Check this fiddle: http://jsfiddle.net/sL4fLnhn/

   $('select').on('change', function(){
      $('select option:disabled').remove();
  });
   $('select').select2();

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