php制作搜索框_jquery+php实现搜索框自动提示

jquery+php实现搜索框自动提示

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

#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;}

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

$(function(){

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

$('#search input[name="k"]').keyup(function(){

$.post('search_auto.php',{'value':$(this).val()},function(data){

if(data=='0') $('#search_auto').html('').css('display','none');

else $('#search_auto').html(data).css('display','block');

});

});

});

服务器端完整php代码:

?

1

2

3

4

5

6

7

8

9

$v=$_POST[value];

$re=mysql_query("select * from test where title like '%$v%' order by addtime desc limit 10");

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

echo '

  • ';

while($ro=mysql_fetch_array($re)) echo '

'.$ro[title].'';

echo '

关闭';

echo '

';

?>

非常实用的功能吧,而且对提升用户的体验度、友好度非常棒,小伙伴们可以结合本文,自由发挥下,加入到自己的项目中去.

【jquery+php实现搜索框自动提示】相关文章:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个基本的实现示例,您可以根据自己的需求进行修改和扩展: HTML代码: ``` <input type="text" id="search" autocomplete="off"> <ul id="suggest"></ul> ``` CSS代码: ``` #suggest { position: absolute; top: 30px; left: 0; width: 300px; max-height: 200px; overflow-y: auto; background-color: #fff; border: 1px solid #ccc; padding: 0; margin: 0; } #suggest li { list-style: none; padding: 5px; cursor: pointer; } #suggest li:hover { background-color: #f5f5f5; } ``` jQuery代码: ``` $(function() { var $input = $('#search'); var $suggest = $('#suggest'); var selectedIndex = -1; $input.on('keyup', function(e) { var keyword = $.trim($input.val()); if (keyword === '') { $suggest.hide(); return; } if (e.keyCode === 38 || e.keyCode === 40) { // Up or Down arrow key var $listItems = $suggest.find('li'); if (e.keyCode === 38 && selectedIndex > 0) { selectedIndex--; } else if (e.keyCode === 40 && selectedIndex < $listItems.length - 1) { selectedIndex++; } $listItems.removeClass('selected'); $listItems.eq(selectedIndex).addClass('selected'); return; } $.ajax({ type: 'GET', url: 'suggest.php', data: { keyword: keyword }, dataType: 'json', success: function(data) { var html = ''; if (data.length > 0) { $.each(data, function(i, item) { html += '<li>' + item + '</li>'; }); } else { html = '<li>No suggestions found</li>'; } $suggest.html(html).show(); selectedIndex = -1; } }); }); $suggest.on('mousedown', 'li', function() { var text = $(this).text(); $input.val(text); $suggest.hide(); }); $(document).on('mousedown', function(e) { var $target = $(e.target); if (!$target.is($input) && !$target.is($suggest)) { $suggest.hide(); } }); }); ``` 说明: 1. 用户在搜索框输入关键词时,会触发 `keyup` 事件。 2. 如果关键词为空,或者用户按下了 Esc 键,就隐藏建议框。 3. 如果用户按下了 Up 或 Down 键,就在建议框中选中上一个或下一个选项,并修改搜索框中的文本。 4. 如果用户输入了有效的关键词,就通过 AJAX 请求获取相关的建议列表,并在建议框中显示出来。 5. 当用户点击建议框中的某个选项时,就将该选项的文本复制到搜索框中,并隐藏建议框。 6. 当用户在页面上点击其他地方时,就隐藏建议框。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值