又是一个jQuery自动完成线程

Yet another jQuery autocomplete thread

本文关键字:线程 jQuery 一个      更新时间:2023-09-26

我正在试图找出我的代码(Laravel5项目)的问题。

到目前为止我有:master.blade.php

<!-- jQuery Version 1.11.0 and jQuery UI-->
<link rel="stylesheet" href="http://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="{{asset("patho/to/jquery/jquery-1.11.0.js")}}"></script>
<script src="http://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>

主视图在任何视图中都是预加载的,它用作页眉和页脚包装器。注意,我混淆了本地路径到jQuery脚本,它是真实的,它被加载,它的工作。

包含在我的一个CRUD视图中:create.blade.php

{!! Form::select('league_id',[], Request::old('league_id'), array('class' => 'form-control', 'id' => 'league_name')) !!}
<script src="{{ asset('js/tips.js') }}">

在我的tips.js文件中:

$(document).ready(function() {
    var dataSource = ['test1', 'test2'];
    $('#league_name').autocomplete(
        {
            source: dataSource
        }
    );
});

我还试过:

var dataSource = [{value: 'test1', name: 'test1'}]

但是它仍然不工作,也没有显示控制台错误。

什么线索吗?

编辑:遵循michaelbahr的建议:

自动完成小部件根本不执行,我得到的结果选择下拉框只是一个空的。没有项目,没有小部件的搜索框。一个普通的空select:

<select class="form-control ui-autocomplete-input" id="league_name" name="league_id" autocomplete="off"></select>

这是行不通的。自动完成需要输入(文本)字段才能工作。如果你想要选择,你可以这样做。

但是,如果你已经有一个数组,你不需要自动完成,你可以简单地使用jQuery,像这样:

$(document).ready(function() {
    var dataSource = ['test1', 'test2'];
    var league = $('#league_name');
    $.each(dataSource, function(index, value){
        league.append("<option>" + value + "</option>");
    })
});