0

我的 Select2 下拉菜单有一些问题。我最初只需要在我的 drop dpwn 上选择一个项目,但我现在需要将它作为一个 manytomanyfield,所以 Select2 似乎是最好的选择。

这是我的原始代码

JS

 $.get('/api/foos', function (response){
        $.each(response.foos, function(){
            $('#foo').append('<option>'+this+'</option>')
        })
    })

API

@require_GET
def Foos(request):
return JsonResponse({
    'foos':[x.foo_name for x in FOO.objects.all()]
})

{foos: ["shh", "fgdh", "fgb", "ghfs", "sfgh", "sfgh", "srth"]}

这对于单选非常有效。现在我正在尝试转换为 Select2,但我用它碰壁了我似乎无法在下拉列表中得到任何结果

$.get('/api/foos', function (response){
        $("#set_foo").select2({
            multiple:true,
            placeholder: "Select foos"});
            ('response.foos');
    })

并使用相同的 api 调用

4

1 回答 1

0

解决它

$.get('/api/locations', function (response){
        var data = (response.locations)
        console.log(data)
        $("#set_location").select2({
            data: data,
            multiple:true,
            tags:true,
            placeholder: "Select Locations",
        })  
    })
于 2016-02-05T15:17:55.963 回答