douhoushou8385 2015-11-10 08:03
浏览 82
已采纳

使用Jquery自动完成,远程源进行过滤

So, i have implemented "Jquery Autocomplete" to my input field.

HTML

<input type="text" id="objekt_nr">

JS

$(function() {
    $( "#objekt_nr" ).autocomplete({
        source: 'file.php',
        minLength: 2,
    });
});

After some reading (no info in the docs) on google, i understand that you need to have the filter in "Server Side" php script. And i use some filtering there.

PHP

$term = trim(strip_tags($_GET['term']));
$query = "SELECT DISTINCT column1 as value FROM table WHERE column1 LIKE '".$term."%'";

My problem is that the "catch" is only filtering the very first or maby second charater.
And as i type, the miss fitting characters won´t filter out.

Example: I type in 2133.. no i can also see: 2135..
How can i solve this?
Do i need to write my own on keyup, function?
Because i cannot find anything about this in the docs.

  • 写回答

1条回答 默认 最新

  • doucan9079 2015-11-10 08:12
    关注

    If I understand correctly, you want it to filter based on partial matches ( eg when you type 2133, to show results for 2, 21, 213, and 2133 ).

    Assuming the above is true, you don't need to do anything in jQuery - entirely in PHP. So, modifying your code above :

    $term = trim(strip_tags($_GET['term']));
    for($i=1; $i<=strlen($term); $i++) {
      $test_term = substr($term, 0, $i);
      $query = "SELECT DISTINCT column1 as value FROM table 
                 WHERE column1 LIKE '".$test_term."%'";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题