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);
                Try this :
$("#selectId").val(81).trigger('change');
Instead of
$("#selectId").val(81);
                        try below code:
var $example = $("#selectId").select2();
$example.val(81).trigger("change"); 
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With