How to set select value in select2 plugin - jquery

陌路散爱 提交于 2019-12-10 06:35:24

问题


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);

回答1:


Try this :

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

Instead of

$("#selectId").val(81);



回答2:


try below code:

var $example = $("#selectId").select2();
$example.val(81).trigger("change"); 



回答3:


$("#MyDropdownList").select2("trigger", "select", {
                        data: { id: theID, text: theText }
                    });


来源:https://stackoverflow.com/questions/38873508/how-to-set-select-value-in-select2-plugin-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!