I have multiple choices, for example:
<select multiple="multiple" class="myList"> <option value="1" selected="selected">Apple</option> <option value="2" selected="selected">Mango</option> <option value="3" selected="selected">Orange</option> </select>
Now, in addition to the parameters that should be selected in the selection window, I need an additional ajax function that would give values โโfrom a remote source.
Here is my code for select2
$(function(){ $(".myList").each(function(){ $(this).select2({ placeholder: "Search for fruits", minimumInputLength: 2, multiple: true, id: function(e) { return e.id+":"+e.name; }, ajax: { url: "https://localhost:8443/fruit_search", dataType: 'json', data: function(term, page) { return { q: term }; }, results: function(data, page) { var frts=[]; $.each(data,function(idx,Frt){ frts[frts.length]=Frt; }); return { results: frts }; } }, initSelection: function(element, callback) { var data = []; }, formatResult: formatResult, formatSelection: formatSelection }); }); });
But I get an error message:
Error: the 'id' option is not allowed for Select2 when connecting to <select> .
But when I use <input type="hidden"> , then where should I store the preselected parameters? How to show them when select2 window appears?
source share