当页面加载时,我将一些数据加载到选择框中,并通过ajax请求返回之前已选择的json字段。
我如何实现select2将某些项目设置为使用ajax请求进行选择?
我的json仅返回ID和文本。
我的ajax请求,尚无任何功能:
// Get permissions
$.ajax({
type: "Get",
url: "/Files/GetFolderPermissions",
data: { folderId: folderId }
}).success(function (data) {
// Do stuff
//$('#EditFolder #FolderActionModel_ValueRole')
}).error(function () {
alert('Ajax call is mislukt, kon de rollen niet ophalen');
}).always(function () {
hideLoadingScreen();
});
这是我为您制作的一些示例,
具体取决于您的select2格式
$("#yourSelect2Selector").select2({
id: function(e) {return e.name;},
placeholder: "Place holder is here",
allowClear: true,
width: 'resolve',
initSelection : function (element, callback) {callback([]);},
formatResult: function(item) {return item.name;},
formatSelection: function(item) {
return item.name;
}
});
}
设置使用json对象选择
$("#yourSelect2Selector").select2("data", {name: 'My name is ...'});
或者将数据作为字符串
$("#yourSelect2Selector").select2("data",'Your data is here')
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句