自动完成时出现问题

时间:2012-08-10 15:46:34

标签: php javascript jquery mysql

我正在建立一个网站来学习编码,我有一个基于Jquery ui的自动完成,我正在填充三个mysql表。

这是我在index.php上的代码(我的搜索框所在的页面和自动填充应该在的页面)

    <script src="./public/js/jquery.js"></script>
    <script src="public/js/jquery-ui-1.8.22.custom.min.js" type="text/javascript" charset="utf-8"></script>
    <script>
    $(function() {

            $("#search").autocomplete({
            source: "suggest.php",
            minLength = 2,
            select: function( event, ui ) {
                    log( ui.item ?
                         "Selected: " + ui.item.value + aka " + ui.item.id :
                         "Nothing selected, input was " + this.value );
                    }
            });

    });
    </script>

继承index.php上的代码,格式为:

<form class="form-search span8 offset6">
    <input type="text" id='search' name='q' autocomplete="off" class="input-medium search-query">
    <button type="submit" class="btn btn-warning">GO!</button>
</form>

以下是关于suggest.php的代码:

<?php

require("./config.php");

$q = $_GET['q'];

$names = '';

$result = mysql_query("SELECT name FROM company WHERE name LIKE '$q%' UNION SELECT cat FROM cat WHERE cat LIKE '$q%' UNION SELECT subcat FROM subcat WHERE subcat LIKE '$q%' LIMIT 10");

$names = array();

while ($row = mysql_fetch_array($result)) { 
   $names[] = $row['name']; 
}

echo json_encode($names);
?>

如果我直接去建议.php?q = SOMETHING 并回显$ name,这就是出现的结果:

["City Market","Cafes","Cheesesteaks","Chicken Wings","Chinese","CSA","Coffee & Tea","Convenience Stores","Comedy Clubs"]Array

当我在Chrome中打开开发人员面板时,自动提示的jQUery旁边出现错误“Uncaught SyntaxError:Unexpected Token =”

我的autosuggest没有显示。什么错了?

感谢您的帮助!


我将=更改为:现在新错误是“意外令牌非法”并且自动填充仍未显示...有任何建议吗?

1 个答案:

答案 0 :(得分:8)

你在minLength中有一个等号,应该是冒号

$(function() {

        $("#search").autocomplete({
        source: "suggest.php",
        minLength: 2,
        select: function( event, ui ) {
                log( ui.item ?
                     "Selected: " + ui.item.value + aka " + ui.item.id :
                     "Nothing selected, input was " + this.value );
                }
        });

});