我正在建立一个网站来学习编码,并且我有一个基于 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);
?>
如果我直接访问Suggest.php?q= SOMETHING并回显$names,结果如下:
["City Market","Cafes","Cheesesteaks","Chicken Wings","Chinese","CSA","Coffee & Tea","Convenience Stores","Comedy Clubs"]Array
当我在 Chrome 中打开开发人员面板时,jQUery 旁边有一个错误,自动提示说“Uncaught SyntaxError: Unexpected Token =”
我的自动建议没有出现。怎么了?
感谢大家的帮助!
我将 = 更改为 : 现在新错误是“Unexpected Token Illegal”并且自动完成仍然没有出现......有什么建议吗?