javascript - 使用 php 自动完成文本框

标签 javascript php jquery ajax autocomplete

我正在使用 PHP 进行自动完成,但我在使用以下代码时遇到错误,所以请帮助我。

INDEX.PHP

这是我的HTML代码

<form method="POST">
    <input type="text" name="txtpname" id="txtpname" size="30" class="form-control" placeholder="Please Enter City or ZIP code">
</form>

这是我的脚本

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.18/jquery-ui.min.js"></script>
<link rel="stylesheet" type="text/css" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" />

<script type="text/javascript">
    $(document).ready(function(){
        $("#txtpname").autocomplete({
            source:'ajax_autocomplete_party.php',
            minLength:1
        });
    });
</script>

这是我从中获取数据的 ajax 文件。 ajax_autocomplete_party.php 包括“脚本/db.php”;

$term=$_GET["txtpname"];

 $query=mysql_query("SELECT * FROM party_details where NAME like '%".$term."%' order by NAME");
 $json=array();

while($party=mysql_fetch_array($query))
{
    $json[]=array(
        'value'=> $party["PARTY_ID"],
        'label'=>$party["NAME"]
    );
}

echo json_encode($json);

当我的页面重新加载时出现错误:autocomplete not defined 现在该怎么办

最佳答案

最新版本的 jQuery 和 -UI 使其工作:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

$(document).ready(function() {
  $("#txtpname").autocomplete({
    source: [
      "ActionScript",
      "AppleScript",
      "Asp",
      "BASIC",
      "C",
      "C++",
      "Clojure",
      "COBOL",
      "ColdFusion",
      "Erlang",
      "Fortran",
      "Groovy",
      "Haskell",
      "Java",
      "JavaScript",
      "Lisp",
      "Perl",
      "PHP",
      "Python",
      "Ruby",
      "Scala",
      "Scheme"
    ],
    minLength: 1
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<link rel="stylesheet" type="text/css" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/themes/base/jquery-ui.css" />

<form method="POST">
  <input type="text" name="txtpname" id="txtpname" size="30" class="form-control" placeholder="Please Enter City or ZIP code">
</form>

关于javascript - 使用 php 自动完成文本框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46888778/

相关文章:

javascript - TypeScript 中的 "declare class"和 "interface"有什么区别

PHP 到 CSV 换行问题

javascript - 加载前修改 &lt;script&gt; 标签中的 "src="- 无需 jQuery

javascript - 针对群组的 Facebook API 响应

javascript - 数组 - 在两个日期之间过滤

php - 将 index.php 重定向到 root 不适用于 SSL

javascript - 如何将 Nivo Slider 缩小到移动尺寸?

javascript - Bootstrap 按钮组更改 jQuery

javascript - 从表 TR id 和 TD id 获取值

php - 连接到 comment_text() 以添加周围的标签