Jquery Autocomplete以第一个字母开头

时间:2013-05-21 10:45:08

标签: jquery jquery-ui

这是我的代码

<div>
    <label for="dynamicSearch">Search:</label>
    <input id="dynamicSearch">
</div>

$(function () {
 // autocomplete
    $("#dynamicSearch").autocomplete({
        source: "dynamicSearch.php",
        minLength: 1
    });
});

dynamicSearch.php

<?php
$con = pg_connect("connection");
if (!$con)
  {
  die("Could not connect: " . pg_last_error());
  }

  $dynamicSearch = "SELECT name from bkash_dist WHERE name LIKE '%".$_GET['term']."%' LIMIT 10";

  $result = pg_query($con, $dynamicSearch);



    while($row = pg_fetch_array($result))
      {      
            $results[] = array('label' => $row['distrbutor']);
            }



pg_close($con);
$encoded = json_encode($results); 

// send response back to index.html
// and end script execution
die($encoded)
?>

当我搜索“A ”时,它会给出所有结果,其中包含“ A ”,但我希望那些以“开始 > A “或” a “(区分大小写)。我该怎么办?

2 个答案:

答案 0 :(得分:3)

只需更改 SQL查询,使其仅查找结果的开头:

$dynamicSearch = "SELECT name from bkash_dist 
                  WHERE name
                  LIKE '"$_GET['term']."%'
                  LIMIT 10";

答案 1 :(得分:1)

你的dynamicSearch.php中有什么?

看看this example in the documentation。它完全符合您的要求