几天来,我一直在尝试让 jQuery 自动完成功能正常工作。现在我已经可以在文本框中输入它,它准确地显示了我想要的内容,但我的问题是当我点击我想要的那个时,它没有显示在文本框中。我知道这与自动完成有关,但我一生都无法弄清楚。如果有人可以提供帮助,将不胜感激。
索引.php
<?php require('_php/functions.php') ?>
<!DOCTYPE HTML>
<html><head>
<title>simple_light</title>
<link rel="stylesheet" href="_style/main.css" />
</head>
<body>
<h1> Autocomplete with jQuery</h1>
<input type="text" name="autosuggest" id="autosuggest" onKeyUp="autoSuggest();"/>
<div id="autosuggest-container"></div>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="_scripts/functions.js"></script>
</body>
</html>
函数.js
function autoSuggest(){
var autosuggestVal = $('#autosuggest').val();
if (autosuggestVal != ''){
$.ajax({
url: '_php/ajax/autosuggest.php?query='+autosuggestVal,
success: function(result){
$('#autosuggest-container').html(result);
}
});
}
}
函数.php
<?php
require('config.php');
function autoSuggest($query){
$connect = mysql_connect(DB_HOST, DB_USER, DB_PASS) or die(mysql_error());
mysql_select_db(DB_NAME, $connect);
$sql = "
SELECT Location.LocationID, CONCAT_WS(' ', SystemName, SiteNameLocation, SiteAddress1, SiteCity, SiteProvince, SitePostalCode) as expr2
FROM Location
WHERE Location.SystemName like '%".mysql_real_escape_string($query)."%'
OR Location.SiteNameLocation like '%".mysql_real_escape_string($query)."%'
OR Location.SiteAddress1 like '%".mysql_real_escape_string($query)."%'
OR Location.SiteCity like '%".mysql_real_escape_string($query)."%'
OR Location.SiteProvince like '%".mysql_real_escape_string($query)."%'
OR Location.SitePostalCode like '%".mysql_real_escape_string($query)."%'
";
$result = mysql_query($sql, $connect);
$totalRows = mysql_num_rows($result);
if ($totalRows > 0){
$items = '<ul class="autosuggest">';
while ($row = mysql_fetch_assoc($result)){
$items .= '<li>'.$row['expr2'].'</li>';
}
$items .= '</ul>';
}
else{
$items = 'no results found';
}
echo $items;
}
?>
自动建议.php
<?php
require('../functions.php');
autoSuggest($_GET['query']);
?>