调用百度请求 边输入边提示 jQuery UI 自动完成(Autocomplete)支持中文 超简单



index.html

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>jQuery UI 自动完成(Autocomplete) - 默认功能</title>
  <link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
  <script src="//code.jquery.com/jquery-1.9.1.js"></script>
  <script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
  <link rel="stylesheet" href="http://jqueryui.com/resources/demos/style.css">
  <script>
  $(function() {
        $("#q").autocomplete({source: "search.php" }); 
  });
  </script>
</head>
<body>
<div class="ui-widget">
  <input id="tags">标签:
  <input id="q" name="q" /> 
</div>
</body>
</html>


search.php

<?php
if(isset($_GET['term']))
{
//在百度里输入a   http://suggestion.baidu.com/su?wd=a&action=opensearch 
//得到 ["a",["爱奇艺","acfun","安居客","阿里巴巴","a站","阿里云","adobe flash player","阿里巴巴批发网","爱情公寓","爱奇艺网"]] 转换
//["爱奇艺","acfun","安居客","阿里巴巴","阿里云","阿里巴巴批发网","爱情公寓","爱奇艺网","爱乐之城","阿卡丽的神秘商店"]
	
	$wd=$_GET['term'];
	//$wd=iconv('UTF-8', 'GB2312//IGNORE', $_GET['term']);
	$url='http://suggestion.baidu.com/su?wd='.$wd.'&action=opensearch'; 
	$html = file_get_contents($url);
	$html=iconv('GB2312', 'UTF-8//IGNORE', $html); //将字符串的编码从GB2312转到UTF-8
	$html=substr($html,4+strlen($_GET['term']),strlen($html)-1-(4+strlen($_GET['term'])) );  //转换一下
	echo $html;
	
//	$json = '{"foo": 12345}';
//	$obj = json_decode($json);
//	print $obj->{'foo'}; // 12345
}
?>





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小黄人软件

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值