如何使AJAX搜索的onkeyup与jQuery [英] How to bring ajax search onkeyup with jquery

查看:117
本文介绍了如何使AJAX搜索的onkeyup与jQuery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的脚本调用AJAX

<script language="javascript">
function search_func(value)
{
            $.ajax({
               type: "GET",
               url: "sample.php",
               data: {'search_keyword' : value},
               dataType: "text",
               success: function(msg){
                           //Receiving the result of search here
               }
            });
}
</script>

HTML

   <input type="text" name="sample_search" id="sample_search" onkeyup="search_func(this.value);">

问:同时的onkeyup我使用Ajax来获取结果。一旦阿贾克斯结果延迟增加时,我的问题。

Question: while onkeyup i am using ajax to fetch the result. once ajax result delay increases problem occurs for me.

例如: 打字时 T 关键字我收到阿贾克斯的结果,而输入 TE 我收到阿贾克斯结果 当在两个KEYUP一段AJAX的时间延迟,使一个严重的问题。

For Example while typing t keyword i receive ajax result and while typing te i receive ajax result when ajax time delay between two keyup sometime makes a serious issue.

当我键入 TE 快速度。 AJAX搜索 T 关键字来晚了,当比较 TE 。我不知道如何处理这类案件。

When i type te fastly. ajax search for t keyword come late, when compare to te. i dont know how to handle this type of cases.

结果 当输入 TE 关键字快速度,由于阿贾克斯延迟。结果为 T 关键字来了。

Result While typing te keyword fastly due to ajax delays. result for t keyword comes.

我相信我已经expained最多读者的知识。 请帮我提前。谢谢你。

I belive i had expained up to reader knowledge. Please help me in advance. Thank you.

推荐答案

您应该检查值随时间的变化:

You should check if the value has changed over time:

$(function () {
    var minlength = 3;

    $("#sample_search").keyup(function () {
        var that = this,
        value = $(this).val();

        if (value.length >= minlength ) {
            $.ajax({
                type: "GET",
                url: "sample.php",
                data: {
                    'search_keyword' : value
                },
                dataType: "text",
                success: function(msg){
                    //we need to check if the value is the same
                    if (value==$(that).val()) {
                    //Receiving the result of search here
                    }
                }
            });
        }
    });
});

这篇关于如何使AJAX搜索的onkeyup与jQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆