Skip to content Skip to sidebar Skip to footer

Select2 Removes Default Options When Using Ajax

I am trying to setup my pre defined select options in select2 with ajax but when I am using the ajax and clicking on the select2 it removes the html options I had there before, an

Solution 1:

In your server side script, where you are processing the search query the user sent, return the first 5 elements which should appear as default, even if the query is empty.

 $query=$_REQUEST['q'];
 if (!empty($query)) {
     //populate the results if the user is typed something in
 } else {
   $results=array();
   $results[0]['text']=Alabama;
   $results[0]['id']="idofalabama";
   // and so on, for the number of results you want to display when the user didn't input anything in the search box
 }
 return json_encode($results);

Post a Comment for "Select2 Removes Default Options When Using Ajax"