我在网上某处找到了这段代码,并根据我的需要进行了调整。
嗨,可能是我的其余代码将有助于澄清它:
index.php>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="searchdb.js"></script>
<div>DBLookUp <input id="autocomplete" type="text" />
<div id="showresults"></div>
</div>
searchdb.js&gt;
//autocomplete function
$(document).ready(function(){
$("#autocomplete").keyup(function()
{
$("#showresults").show();
var dbsearch;
dbsearch = $("#autocomplete").val();
if (dbsearch.length > 0)
{
$.ajax(
{
type: "POST",
url: "autocomplete.php",
data: "dbsearch=" + dbsearch,
success: function(message)
{
$("#showresults").empty();
if (message.length > 0) {
$("#showresults").append(message);
} else {
$("#showresults").append("No Results found!");
}
}
});
}
else
{
// if empty
$("#showresults").empty();
}
});
});
//自动完成功能的结束
function selectItem(id,value)
{
document.getElementById("autocomplete").value=value;
autocomplete(id,value);
}
autocomplete.php&gt;
<?php
require_once("searchdb.php");
$dbsearch = strtolower(mysql_real_escape_string(strip_tags($_POST['dbsearch'])));
$autocompl=new DBQUERY();
$autocompl->autocomplete($dbsearch);
?>
searchdb.php&gt;
<?php
class DBQUERY {
private $dbh;
public function __construct() {
$this->dbh = new PDO("mysql:host=localhost;dbname=mydb", "root", "");
}
public function autocomplete($dbsearch)
{
$cons = $this->dbh->prepare("select * from clients where cl_name like :dbsearch");
$cons->execute(array(':dbsearch' => '%'.$dbsearch.'%'));
while($dbrow = $cons->fetch())
{
$data=$dbrow["cl_name"];$id=$dbrow["cli_id"];
echo "<a class='resultsdb' href=\"javascript:selectItem
(".$id.",'".$data."')\">".$data."</a><br>";
}
?>
<script type="text/javascript">
$(document).ready(function(){
$(".resultsdb").click(function(){
$("#showresults").hide();
});
});
</script>
<?php
}
//end autocomplete
}
?>
效果很好,但我不能让它显示“没有找到结果!”信息。 我认为else子句被if语句覆盖,if语句在检测到输入后立即开始工作。 (... if(dbsearch.length&gt; 0)...)
但是因为我刚接触到jquery,我似乎无法弄清楚在哪里更改脚本以使其正常工作。
答案 0 :(得分:0)
也许你应该查看$ cons:
public function autocomplete($dbsearch) {
$cons = $this - > dbh - > prepare("select * from clients where cl_name like :dbsearch");
$cons - > execute(array(':dbsearch' = > '%' .$dbsearch. '%'));
if (count($cons) > 0) {
while ($dbrow = $cons - > fetch()) {
$data = $dbrow["cl_name"];
$id = $dbrow["cli_id"];
echo "<a class='resultsdb' href=\"javascript:selectItem (".$id.",'".$data."')\">".$data."</a><br>";
} ?>
} else {
$data = '';
}
<script type = "text/javascript">
$(document).ready(function() {
$(".resultsdb").click(function() {
$("#showresults").hide();
});
}); </script>
<?php
}
// end autocomplete
} ?>
答案 1 :(得分:0)
if (message.trim() != "") {
//message = message; <-- not needed..
$("#showresults").append(message);
}else{
//message = "No Results found!"; <-- also not needed..
$("#showresults").html("No Results found!");
}
你必须从你的php返回空字符串... 而且,这不应该在那里......
<script type="text/javascript">
$(document).ready(function(){
$(".resultsdb").click(function(){
$("#showresults").hide();
});
});
</script>
把这个放在不像php这样的文档中:
//autocomplete function
$(document).ready(function(){
$("#showresults").on("click", ".resultsdb", function(){
$("#showresults").hide();
});
$("#autocomplete").keyup(function()
//....