开发者

jQuery UI auto-complete auto-suggestion list does not change

开发者 https://www.devze.com 2023-02-21 22:28 出处:网络
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.

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.

main file:

<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/javascri开发者_Go百科pt">
    $(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>

result file

<?php
$arrResults = array('orange', 'apple', 'bannana');
// Print them out, one per line
echo json_encode($arrResults);    
?>


You need something like this

$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);


$("#tags").autocomplete({source:"result.php", dataType:"json"});
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号