dtr32221 2014-12-06 22:12
浏览 27
已采纳

如何使用ajax自动完成[关闭]

im using autocomplete of jqueryui (http://jqueryui.com/autocomplete/#remote) and the source comes from "source: "search.php""

This code ...

 $( "#name" ).autocomplete({
source: "search.php",
minLength: 2,
select: function( event, ui ) {
log( ui.item ?
"Selected: " + ui.item.value + " aka " + ui.item.id :
"Nothing selected, input was " + this.value );
}
}); 

.. And this code below together works perfectly fine!
but autocomplete suggest me "All of my Data" ! not smth like my term.

<?php
require('inc/tunel.php');
$keyword = $_POST['keyword'];
$respons = array();
$fetch_engine = $db->query("SELECT name FROM `engine`");
    while($read_engine = $fetch_engine->fetch(PDO::FETCH_ASSOC)) {
        $respons[] = $read_engine['name'];
        }

        echo json_encode($respons);
 ?>

i also tried this code below, my problem solved, but i dont want to see my data in "view source" of my document!

$( "#name" ).autocomplete({
source: <?php
    require('inc/tunel.php');

    $respons = array();
    $fetch_engine = $db->query("SELECT name FROM `engine`");
        while($read_engine = $fetch_engine->fetch(PDO::FETCH_ASSOC)) {
            $respons[] = $read_engine['name'];
            }
            
            echo json_encode($respons);
            

?>,
minLength: 2,
select: function( event, ui ) {
log( ui.item ?
"Selected: " + ui.item.value + " aka " + ui.item.id :
"Nothing selected, input was " + this.value );
}
}); 

</div>
  • 写回答

1条回答 默认 最新

  • douqin3245 2014-12-06 22:46
    关注

    You should create a local service that accepts a parameter with a substring (in my example parameter q with at least 2 chars) and returns a json with the possible tags, and then use it into the source of the autocomplete as an ajax call.

    
        $( "#name" ).autocomplete({
              source: function( request, response ) {
               $.ajax({
                  url: "http://yourwebservice.com/search",
                  dataType: "jsonp",
                  data: {
                    q: request.term
                  },
                  success: function( data ) {
                    response( data );
                  }
                });
              },
              minLength: 2
            });
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 正弦信号发生器串并联电路电阻无法保持同步怎么办
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)