在@Camille的帮助下找到了解决方案。
-
不必要的重新处理json文件(仅当您的json文件使用的标识符不同于
"id"
和
"text"
-
json文件的格式不正确,请参阅
https://select2.org/data-sources/formats
代码:
// Create countries dropdown
$('#allowedCountries').select2({
placeholder: 'Select allowed countries',
selectOnClose: false,
tags: false,
tokenSeparators: [',', ' '],
ajax: {
dataType : "json",
url : "includes/countries.json",
},
});
国家。json
{
"results": [
{
"id": 1,
"text": "Afghanistan"
},
{
"id": 2,
"text": "Aland Islands"
},
{
"id": 3,
"text": "Albania"
},
...
{
"id": 245,
"text": "Yemen"
},
{
"id": 246,
"text": "Zambia"
},
{
"id": 247,
"text": "Zimbabwe"
}
]
}