Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set select value in select2 plugin - jquery

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);
like image 1000
Hüseyin Okumuş Avatar asked Nov 29 '22 00:11

Hüseyin Okumuş


2 Answers

Try this :

$("#selectId").val(81).trigger('change');

Instead of

$("#selectId").val(81);
like image 124
Joe. L Avatar answered Dec 05 '22 22:12

Joe. L


try below code:

var $example = $("#selectId").select2();
$example.val(81).trigger("change"); 
like image 39
Prashant Patil Avatar answered Dec 06 '22 00:12

Prashant Patil