jquery+php实现用户输入搜索内容时自动提示

index.html

<html>
<head>
    <meta charset= " utf-8 ">
<style>
#search{font-size:14px;}
#search .k{padding:2px 1px; width:320px;}
#search_auto{border:1px solid #817FB2; position:absolute; display:none;}
#search_auto li{background:#FFF; text-align:left;}
#search_auto li.cls{text-align:right;}
#search_auto li a{display:block; padding:5px 6px; cursor:pointer; color:# 666;}
#search_auto li a:hover{background:#D8D8D8; text-decoration:none; color:# 000;}
</style>
<title>jquery+php实现用户输入搜索内容时自动提示</title>
</head>
<body>
<div id= " search ">
<input type= " text " name= " k " /> <input type= " button " name= " s " value= " 搜索 " />
</div>
<div id= " search_auto "></div>

<script src= " jquery.min.js "></script>
<script>
$(function(){

    $( ' #search_auto ').css({ ' width ':$( ' #search input[name="k"] ').width()+ 4});
    $( ' #search input[name="k"] ').keyup(function(){
        value = $( this).val();

        $.post( ' search_auto.php ',{ ' value ':$( this).val()},function(data){
            console.log(data);
             if(data== ' 0 ') $( ' #search_auto ').html( "").css( ' display ', ' none ');
             else $( ' #search_auto ').html(data).css( ' display ', ' block ');
        });
    });

});
</script>

</body>
</html>

===================================================== 
 
search_auto.php

 
<?php

    $v=$_POST[ ' value '];

     $conn=mysql_connect( " servername  "" username "" password ");

     $re=mysql_query( " dataname "" select * from `db_table` where title like '%$v%' order by addtime desc limit 10 ", $conn);

     if(mysql_num_rows($re)<= 0) exit( ' 0 ');

    echo  ' <ul> ';

     while($ro=mysql_fetch_array($re)) echo  ' <li><a href=""> '.$ro[ ' title ']. ' </a></li> ';

    echo  ' <li><a href="javascript:;" οnclick="$(this).parent().parent().parent().fadeOut(100)">关闭</a& amp; gt;</li> ';

    echo  ' </ul> ';

?>
 

 

转载于:https://www.cnblogs.com/qhorse/p/4688445.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值