Reputation: 157
I use this code for insert data to select
element with select2
plugin:
$.ajax({
type: "POST",
url: "ws.asmx/GetEvrakGrup",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (dc, status) {
jsonData = JSON.parse(dc.d);
$("#selectId").select2({
data: jsonData
});
},
error: function () { alert("This is an Error");}
});
After I want to set a value in this select but its not working:
$("#selectId").val(81);
Upvotes: 5
Views: 28726
Reputation: 1011
$("#MyDropdownList").select2("trigger", "select", {
data: { id: theID, text: theText }
});
Upvotes: 6
Reputation: 407
Try this :
$("#selectId").val(81).trigger('change');
Instead of
$("#selectId").val(81);
Upvotes: 18
Reputation: 2573
try below code:
var $example = $("#selectId").select2();
$example.val(81).trigger("change");
Upvotes: 5