【问题标题】:typeahead remote only returning one item预输入遥控器仅返回一项
【发布时间】:2018-01-29 20:32:23
【问题描述】:

我无法让预先输入为我工作。

我的html代码是这样的:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>

    <script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
    <script src="/js/typeahead.bundle.min.js"></script>



</head>

<body>


<div id="remote">
    <input class="typeahead" type="text" placeholder="Oscar winners for Best Picture">
</div>

<script>
    $(document).ready(function () {


        var bestPictures = new Bloodhound({
            datumTokenizer: Bloodhound.tokenizers.obj.whitespace('value'),
            queryTokenizer: Bloodhound.tokenizers.whitespace,
            remote: {
                url: '/service-autocomplete.php'
            }
        });



        $('#remote .typeahead').typeahead(null, {
            name: 'best-pictures',
            display: 'value',
            source: bestPictures
        });
    });
</script>
</body>
</html>

我的服务 (service-autocomplete.php) 包含以下内容:

<?php




$data = [
    [
        "value" => "amelie",
    ],
    [
        "value" => "anastasia",
    ],
    [
        "value" => "paul",
    ],
    [
        "value" => "pierre",
    ],
];

header('Content-Type: application/json');
echo json_encode($data);

现在无论我在输入中输入什么(1 个字母或多个字母),都只会出现第一个建议 (amelie)。

我希望 typeahead 显示所有结果:amelie、anastasia、pierre、paul。

我做错了什么?

【问题讨论】:

    标签: typeahead.js


    【解决方案1】:

    我发现了这个名为 jquery.typeahead 的插件: http://www.runningcoder.org/jquerytypeahead/

    它可以满足我的所有需求,甚至更多,而且它可以立即运行。 所以,我切换到 jquery.typeahead。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-25
      • 2014-10-28
      • 2016-08-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多