jQuery UI自动完成自动建议列表不变 [英] jQuery UI auto-complete auto-suggestion list does not change
本文介绍了jQuery UI自动完成自动建议列表不变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
下面是我的jQuery UI代码,我面临的问题是,无论用户输入什么内容,它都会立即显示数组中的所有内容,而不仅仅是显示靠近输入的单词.
Below are my code for jQuery UI, the problem I am facing is that no matter what the user inputs it immediately displays everything in the array and not just the words close to the inputs.
主文件:
<html>
<head>
<link type="text/css" href="jqui/css/ui-lightness/jquery-ui-1.8.11.custom.css" rel="stylesheet" />
<script type="text/javascript" src="jqui/js/jquery-1.5.1.min.js"></script>
<script type="text/javascript" src="jqui/js/jquery-ui-1.8.11.custom.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#tags").autocomplete({source:"result.php"});
});
</script>
</head>
<body>
<div class="ui-widget">
<label for="tags">Tags: </label>
<input id="tags">
</div>
</body>
</html>
结果文件
<?php
$arrResults = array('orange', 'apple', 'bannana');
// Print them out, one per line
echo json_encode($arrResults);
?>
推荐答案
您需要类似的东西
$req = $_GET['term']; //first get the search keyword as get method
$arrResults = array('orange', 'apple', 'bannana');
$array = array_filter($arrResults, 'mycallback');
//filter the array containing search word using call back function
function mycallback($var)
{
global $req;
if(preg_match('/^'.$req.'/', $var))
{
return $var;
}
}
$array1 = array();
//filter null array
foreach($array as $arr => $val)
{
if(!empty($val))
{
$array1[] = $val;
}
}
//echo out the json encoded array
echo json_encode($array1);
这篇关于jQuery UI自动完成自动建议列表不变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文