【发布时间】:2020-12-05 15:35:41
【问题描述】:
我正在为我的 django 项目使用 jquery 来自动完成。但是 on select 并没有被解雇。这是我的代码:
<script>
$(function () {
$("#product").autocomplete({
source: '{% url 'discover' %}',
minLength: 2,
select: function(event, ui) {
alert("Selected ");
},
});
});
</script>
我的html代码:
<form action="">
<input id='ticker' type="search" placeholder=" Search company" aria-describedby="button-addon4" class="form-control bg-none border-0" style="font-family:Arial, FontAwesome;height: 60px;width:600px;background-color:rgb(31, 29, 54)">
</form>
以及我使用的脚本:
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
和 django 部分:
def discover(request):
if 'term' in request.GET:
qs = Instrument.objects.filter(name__icontains=request.GET.get('term'))
titles = list()
for instrument in qs:
titles.append(instrument.name)
# titles = [product.title for product in qs]
return JsonResponse(titles, safe=False)
自动完成功能正常,但由于某种原因未触发选择事件。我做错了什么?
【问题讨论】:
标签: javascript jquery django jquery-ui-autocomplete