第一个问题是我有一个自动完成功能,当我完成输入名字时,它可以正常工作,但是当我按空格键继续输入姓氏时,div 会隐藏并且不会继续搜索结果。我遗漏了一些东西在 php 文件或 jquery 文件中。
这是搜索栏
<form method='post' action='index2.php#profile_info.php'>
<input type="text" id='inputSearch' placeholder="Search" autocomplete="off" class="search" />
<input type='submit' id='Search_Submit' value='' />
</form>
<div id="divResult"</div>
这是查询数据库的 search.php 文件
<?php
include('connect.php');
if($_POST)
{
$q=$_POST['searchword'];
$sql_res=mysql_query("SELECT user_id, firstname, lastname, email, country, gender from users where firstname like '%$q%' OR lastname like '%$q%' order by email_activated='1' LIMIT 5");
while($row=mysql_fetch_array($sql_res))
{
$id = $row['user_id'];
$firstname=$row['firstname'];
$lastname=$row['lastname'];
$email=$row['email'];
$country=$row['country'];
$Gender=$row['gender'];
$cacheBuster = rand(999999999,9999999999999);
$check_pic = "members/$id/image01.jpg";
$default_pic = "members/image01.jpg";
if (file_exists($check_pic)) {
$users_pic ="$check_pic?$cacheBuster"; // forces picture to be 100px wide and no more
}else {
$users_pic = "images/profiles/".$Gender."_small.jpg"; // forces default picture to be 100px wide and no more
}
$b_firstname='<b>' .$q. '</b>';
$b_lastname='<b>'.$q.'</b>';
$final_firstname = str_ireplace( $q, $b_firstname, $firstname);
$final_lastname = str_ireplace( $q, $b_lastname, $lastname);
?>
<div class="display_box" align="left" >
<a href="http://localhost/MyNewSite/index2.php?user_id='<?php echo $id; ?>'#status.php" target="_self"> <img src="<?php echo $users_pic; ?>" style="width:55px; height:50px; float:left; margin-right:6px;" /></a><span class="name"><a href="http://localhost/MyNewSite/index2.php?user_id='<?php echo $id; ?>'#status.php" target="_self" class="friendsLink"><?php echo " $final_firstname $final_lastname"; ?> </a></span> <br/><span style="font-size:10px; margin-left:6px;"><?php echo $email; ?></span><br/>
<span style="font-size:10px; color:#C40000; margin-left:6px;"><?php echo $country; ?></span></div>
<?php
}
}
?>
和 jquery 加载 div
$(function(){
$(".search").keyup(function()
{
var inputSearch = $(this).val();
var dataString = 'searchword='+ inputSearch;
if(inputSearch!='')
{
$.ajax({
type: "POST",
url: "search.php",
data: dataString,
cache: false,
success: function(html)
{
$("#divResult").html(html).show();
}
});
}return false;
});
jQuery("#divResult").live("click",function(e){
var $clicked = $(e.target);
var $name = $clicked.find('.name').html();
var decoded = $("<div/>").html($name).text();
});
jQuery(document).live("click", function(e) {
var $clicked = $(e.target);
if (! $clicked.hasClass("search")){
jQuery("#divResult").fadeOut();
}
});
$('#inputSearch').click(function(){
jQuery("#divResult").fadeIn();
});
});
jQuery(function($){
$("#inputSearch").Watermark("Search");
});