java - Jquery + PHP : Grabbing the autocomplete strings from a third party php script

标签 java php jquery ajax

Jquery 搜索示例:

<script>
    $(function() {
        var availableTags = [
            "ActionScript",
            "AppleScript",
            "Asp",
            "BASIC",
            "C",
            "C++",
            "Clojure",
            "COBOL",
            "ColdFusion",
            "Erlang",
            "Fortran",
            "Groovy",
            "Haskell",
            "Java",
            "JavaScript",
            "Lisp",
            "Perl",
            "PHP",
            "Python",
            "Ruby",
            "Scala",
            "Scheme"
        ];
        $("#tags").autocomplete({
            source: availableTags
        });
    });
</script>

<div class="demo">
    <div class="ui-widget">
        <label for="tags">Tags: </label>
        <input id="tags" />
    </div>
</div>
<!-- End demo -->

<div class="demo-description">
    <p>The Autocomplete widgets provides suggestions while you type into the field. Here the suggestions are tags for programming languages, give "ja" (for Java or JavaScript) a try.</p>
    <p>The datasource is a simple JavaScript array, provided to the widget using the source- option.</p>
</div>
<!-- End demo-description -->

基本上有一个带有自动完成内容的变量,这很好,除了我需要一些可能更复杂的东西。我不需要从 var/xml/sql 提供列表,而是需要从第三方 php 脚本发出的 echo 中获取。

该 php 脚本将根据查询回显相应的信息。即:用户搜索 customsearch.php?q=Lemons 它将回显“Pineapples”。

有人可以帮助我吗?

最佳答案

基于您的其他question ,我假设您正在进行 AJAX 调用来获取搜索结果。将它们加载到数组中并在示例中替换它:

<script>
function GetSearchResults(){
    // make your ajax call here
    $.ajax({
      async: false,
      url: 'customsearch.php?q=Lemons',
      success: function(data) {
        var availableTags = [];
        // build an array from the response data here
        $( "#tags" ).autocomplete({
            source: availableTags
        });
      }
    });
}

$(function() {
    var availableTags = GetSearchResults();
});
</script>

<div class="demo">

<div class="ui-widget">
<label for="tags">Tags: </label>
<input id="tags" />
</div>

</div><!-- End demo -->

<div class="demo-description">
<p>The Autocomplete widgets provides suggestions while you type into the field. Here the suggestions     are tags for programming languages, give "ja" (for Java or JavaScript) a try.</p>
<p>The datasource is a simple JavaScript array, provided to the widget using the source-    option.</p>
</div><!-- End demo-description -->

理想情况下,您不会将 async 设置为 false,但如果您不熟悉回调,我会尽力不让您的大脑爆炸。

关于java - Jquery + PHP : Grabbing the autocomplete strings from a third party php script,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8421357/

相关文章:

java - Java中Connection对象什么时候会被清除?

java - Apache Spark,Java 中使用 List<?> 作为第一个参数的 createDataFrame 示例

php - jQueryeach()结果数组到php到数据库

php - 选中从数据库中选择的复选框

javascript - 如果只有一个图像,则停止循环播放幻灯片 + 加载幻灯片时停止第一张图像的淡入淡出

javascript - 如何激活innerHTML中表格响应列的点击选项

java - 将对象转换为java中的接口(interface)?

java - 如何跟踪用户对 Web 应用程序中对象的更改?

ubuntu - nginx + passenger + phpmyadmin = 拒绝访问

javascript - 点击发送数据