Select2, set the default value for Single Select Drop down

I am trying to set the default value for select2 single select dropdown: http://ivaynberg.imtqy.com/select2/ .

I have a select2 dropdown. But I'm not sure how to set the default value. For example, let's say that the list of values ​​is test1, test2, test3. How to set "test1" as the default value for a single selection.

$(function(){
var testData = [{id:0, text:"test1"}, {id:1, text:"test2"}, {id:2, text:"test3"}];

$('#select2customdata').select2({placeholder:"Search Stuff",  data: {results: testData, text:'text'}});

$('#select2customdata').select2().select2("val", "test1");
 });

And html has:

<input type="hidden" id="select2customdata" style="width:300px"> </input>

As you can see, I use select2 ("val", "some-val") to set the default value. This does not work for me.

EDIT: I realized that the solution, if using val, should have an initselection in the parameters. Another solution is to use:

$('#select2customdata').select2("data", {"id": id_val, "text": text_val})
+4
1

document val:

. , id . , .

doc:

, initSelection , Select2 , , val(), , . select, .

.

+5

All Articles