I have select2
ComboBox the data is loaded by Ajax. I am trying to set the default value to the select2
by reading the id
from the input value (which is set on the server side) and set it to the select2
programmatically.
I think I should implement the initSelection()
function in a way to solve this issue.
Here is my code:
The HTML input is:
<input type="hidden" class="mySelect2" value="01" name="someName" />
The value "01" is set at the server
The JavaScript is:
$(".mySelect2").select2({
ajax: {
url:"data.php",
dataType:"json",
data:function(term, page) {
return {
query:term, page: page -1
} ;
},
results:function(data, page) {
return data
}
}
});
I tried this but it did not work .
$(".mySelect2").select2('val', '01');
And an error occured : "cannot call val() if initSelection()
is not defined "
try something like this
HTML CODE