【问题标题】:jQuery and PHP search suggestion scriptjQuery 和 PHP 搜索建议脚本
【发布时间】:2011-05-04 19:09:12
【问题描述】:

我有一个 PHP 和 jQuery 脚本,可以从文本框中创建搜索结果建议。但是,当您在文本框中键入内容、删除它并尝试进行不同的查询时,不会显示任何建议。为什么会这样?

这是我的网页代码的副本:

<script type="text/JavaScript">
function lookup(inputString){
if (inputString.length==0){
$('#suggestions').hide();
} else{
$.post("suggestions.php",{
queryString: "" + inputString + ""},
function(data){
$('#suggestions').html(data);
});
}
}
</script>

<form>
<input type="text" size="30"  onkeyup="lookup(this.value);">
<div id="suggestions"></div>
</form>

这是我的 PHP 代码的副本:

<p id="searchresults"><?php

$db=new mysqli('localhost','username','password','database');

if(isset($_POST['queryString'])){
$queryString=$db->real_escape_string($_POST['queryString']);
            if(strlen($queryString)>0){
                $query = $db->query("SELECT * FROM search s WHERE name LIKE '%" . $queryString . "%'");
                if($query){
                    while ($result = $query ->fetch_object()){
                        echo '<a href="'.$result->name.'">';                        
                        $name=$result->name;            
                        echo ''.$name.'';
                    }
                }
            }
        }
?></p>

提前致谢,卡勒姆

【问题讨论】:

    标签: php jquery html css


    【解决方案1】:

    你隐藏但不再显示。

    将您的回调函数更改为:

    function(data){
        $('#suggestions').html(data).show();
    }
    

    fadeIn() 增加可爱度;)

    【讨论】:

    • $('#suggestions').html(data);之后
    【解决方案2】:

    在您的 else 语句中添加 $('#suggestions').show()

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-09
      • 1970-01-01
      • 1970-01-01
      • 2012-01-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多