dpqmu84646 2012-02-22 23:42
浏览 51
已采纳

php / jquery搜索框

I'm trying to implement a search box using php and jquery (the seach results are coming from a database). What I want to do is that as the user type into the search box, a query is made to find the data that contains the keyword the user entered into the search box. The problem I'm having is that I have something like this:

$("#my_search_box").keyup(function(){

    retrieve_results($(this).val());    

});

The problem with doing this is that, the function retrieve_results() is called every time the user is done typing (of course, cause that's what the keyup is there for). Therefore, this can get very slow because if the user type a word that has like 10 characters, the retrieve_results() function gets called 10 times in a row. Also, I should mention that inside my retrieve_results() function, I'm doing an Ajax request using jQuery $.ajax to get the results. So my question is the following:

How do I make it so that my function retrieve_results() doesn't get executed everytime the user stops typing (every time the key is up). I want to keep that functionality though, I don't want the user to click on a "Search" button or something. I just would like my retrieve_results() function to get called only once when the user is done entering all the keywords (not everytime the user enters a letter or something)

Thank you

  • 写回答

4条回答 默认 最新

  • doudou1309 2012-02-23 02:53
    关注

    Ideally, you only want the search to fire for the very last character the user types. The best way to do this is to give him enough time to continually type, and if he pauses (even to think), run the search. Using setTimeout by itself won't work because even that would fire multiple times. You have to clear the timeout by using clearTimeout.

    You can use the following to achieve the desired effect:

    $(document).ready(function() {
        var timeout,
            $search_box = $('#my_search_box');
    
        $search_box.keyup(function() {
            if (timeout) {
                clearTimeout(timeout);
            }
    
            timeout = setTimeout(function() {
                search();
            }, 1000);
        });
    
        function search() {
            var search_txt = $search_box.val();
        }
    });
    

    Here's what's happening: We assign the setTimeout timer to a variable, which must be declared above your keyup function, and check that variable each time keyup fires. If there's an existing setTimeout in progress, clear it out so that it won't fire the search() function and create a new timer set to wait a specified number of milliseconds (1000 in my example).

    See the following JSFiddle for demo: http://jsfiddle.net/highwayoflife/WE4Fr/

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器