【问题标题】:Jquery UI autocomplete select not firedJquery UI 自动完成选择未触发
【发布时间】: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="&#xF002; 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


    【解决方案1】:

    也许你可以尝试运行,在 DOM 加载完成后,你只有在 DOM 准备好时才执行,你的脚本会是这样的

    <script>
    $(document).ready(function() {
       $("#product").autocomplete({
          source: '{% url 'discover' %}',
          minLength: 2,
          select: function(event, ui) {
             alert("Selected ");
          }
        });
    });
    </script>
    

    【讨论】:

    • 你在
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-05-13
    • 1970-01-01
    • 2018-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-16
    相关资源
    最近更新 更多