【问题标题】:autocomplete works only with spaces自动完成仅适用于空格
【发布时间】:2016-05-31 20:41:04
【问题描述】:

从下面的 jQuery 代码中我已经初始化了 typeahead 插件

var users=new Bloodhound({

    datumTokenizer:Bloodhound.tokenizers.obj.whitespace('username'),
    queryTokenizer:Bloodhound.tokenizers.whitespace,

    remote: {
        url: 'users.php?query=%QUERY',
        wildcard: '%QUERY'
      }

});

users.initialize();

$("#SearchCustomer").typeahead({
    hint: true,
    highlight: true,
    minlength: 2,



},{
    name: 'users',
    display:'username',     
    source:users.ttAdapter(),
    templates: {
        suggestion: function (users) {
        return '<p><a href="index.php?userid='+users.CustomerId+'" style="color:inherit;text-decoration:none;width:100%">'+users.username+'</a></p>';
        }
    }

比如我的数据库是这样的:

1.哈雷
2.哈雷戴维森

当我在搜索框中输入“Har”时,“Harley”只显示在建议中。 “Harley Davidson”仅在我键入“Harley”时显示,末尾有空格。有人可以建议如何克服这个错误吗?

这是我的 users.php 文件

<?php

header('Content-Type:application/json');

if(!isset($_GET['query']))
{
echo json_encode([]);
exit();
}
$mysqli=new PDO("mysql:host=127.0.0.1;dbname=website","root","");

$users=$mysqli->prepare("SELECT id,username FROM users WHERE username LIKE         :query");

$users->execute(['query'=>"{$_GET['query']}%"]);

echo json_encode($users->fetchAll()); 
?>     

【问题讨论】:

  • 我们能看到数据库查询发生的users.php代码吗?您是否调试过 PHP 代码以确保输入到搜索框中的内容就是查询中包含的内容?
  • @Cᴏʀʏ 我已经修改了问题并包含了 users.php 文件
  • 从 url 返回的数据是什么样的?如果使用POSTMan之类的工具调用url,是否有效?

标签: jquery twitter-bootstrap autocomplete typeahead.js


【解决方案1】:

尝试通过这样做将每个单词的第一个字符大写,您的问题将得到解决

将哈雷戴维森更改为哈雷戴维森。对表中的每个实例重复相同的操作...问候

【讨论】:

    【解决方案2】:

    我复制了您的代码并添加了 js 文件和作品。让我展示一下我的代码

    index.php

    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title></title>
        <link rel="stylesheet" href="">
    </head>
    <body>
    <input type="text" id="SearchCustomer">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
        <script src="typeahead.js-master/dist/typeahead.bundle.js"></script>
    <script>
        var users=new Bloodhound({
    
        datumTokenizer:Bloodhound.tokenizers.obj.whitespace('username'),
        queryTokenizer:Bloodhound.tokenizers.whitespace,
    
        remote: {
            url: 'users.php?query=%QUERY',
            wildcard: '%QUERY'
          }
    
    });
    
    users.initialize();
    
    $("#SearchCustomer").typeahead({
        hint: true,
        highlight: true,
        minlength: 2,
    
    
    
    },{
        name: 'users',
        display:'username',     
        source:users.ttAdapter(),
        templates: {
            suggestion: function (users) {
            return '<p><a href="index.php?userid='+users.CustomerId+'" style="color:inherit;text-decoration:none;width:100%">'+users.username+'</a></p>';
            }
        }
    });
    </script>
    
    </body>
    </html>
    

    users.php

    <?php
    
    header('Content-Type:application/json');
    
    $users = array(
        array( 'CustomerId'=> 'example', 'username'=> 'Harley'),
        array( 'CustomerId'=> 'another example', 'username'=> 'Harley Davidson')
    );
    
    echo json_encode($users); 
    ?>
    

    我没有使用数据库,所以可能来自数据库的数据带有一些垃圾。

    我希望这能有所帮助。

    祝你编程愉快!

    更新:

    我更改了 users.php,现在 JSON 没有垃圾了..

    <?php
    
    header('Content-Type:application/json');
    
    if(!isset($_GET['query']))
    {
    echo json_encode([]);
    exit();
    }
    $mysqli=new PDO("mysql:host=127.0.0.1;dbname=example","root","");
    
    $users=$mysqli->query("SELECT id,username FROM Users WHERE username LIKE '%".$_GET['query']."%'");
    
    
    $array = [];
    while($row = $users->fetch(PDO::FETCH_ASSOC)) {
        $array[] = $row;
    }
    
    // echo '<pre>';
    // print_r($array);
    echo json_encode($array); 
    
    ?>   
    

    【讨论】:

    • 只需将“Haryley Davidson”更改为“harley Davidson”并检查代码......它适用于大写,但不适用于小写
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-07
    • 2020-03-02
    • 2017-11-10
    • 2019-12-30
    • 2011-11-07
    • 2017-05-19
    • 2012-10-11
    相关资源
    最近更新 更多