jQuery用户界面自动完成的结果并不在HTML下拉列表显示用户界面、自动完成、结果、列表

2023-09-10 21:55:10 作者:我要的是霸道不是你的迁就

这是我的Django认为Ajax请求的功能:

This is my django view ajax request function:

def get_town(request):
    if request.is_ajax():
        q = request.GET.get('term', '')
        towns = Town.objects.filter(name__icontains=q)
        results = []
        for name in towns:
            name_json = {}
            name_json['name'] = name.name
            results.append(name_json)
        data = json.dumps(results)
    else:
        data = 'fail'
    mimetype = 'application/json'
    return HttpResponse(data, mimetype)

网址,一切都设置得很好。

Urls and everything setup nicely.

我的自动完成是这样的:

My autocomplete is this:

$(function() {
  $("#id_town").autocomplete({
    source: "/api/get_town/",
    minLength: 3,
  });
});

不过,你可以看到从图像下方的结果,虽然从请求返回和使用,不会在下拉列表中显示。按照下面的图像中输入框中输入的按键,我得到的结果是这样的: [{名:Densuano}]

这是什么问题呢?为什么没有下拉显示?

What's the problem then? Why isn't the dropdown showing?

推荐答案

我想通了:

name_json['name'] = name.name

是罪魁祸首。以上是做一个键,值列表,它自动完成,不知何故不能跨preT。我改成

was the culprit. The above was making a key, value list which the autocomplete, somehow couldn't interpret. I changed to

name_json = name.name

和它的工作。