jQuery下拉ajax搜索keydown是"落后一步"

Abh*_*yal 0 javascript php ajax jquery

我有一个下拉列表,通过ajax的php文件回显.网站上的用户可以使用此下拉列表搜索他的朋友.我有以下代码(验证未显示):

var textbox = $('#participant-textbox');
$(function(){
                    textbox.keydown(function(e) {
                        $.ajax({
                            type:"POST",
                            url:"sources/friend_search.php",
                            data:{value:document.getElementById('participant-textbox').value}
                        })

                            .done(function(rtrn) {
                                $('.invite-participants-dropdown').html(rtrn);
                            });

                        if(!DropdownState)
                            {
                                dropdown.show();
                                DropdownState=true;
                            }

                    });
});
Run Code Online (Sandbox Code Playgroud)

虽然这样可行,但是ajax却落后了一步.我的意思是:

当我在文本框中键入"a"时,它不会显示任何内容.

在此输入图像描述

当我输入'ab'时,它应显示'abhishek'(我使用LIKE子句进行搜索),但它只搜索%a%,因此显示所有a名字中包含a的朋友.

在此输入图像描述

当我键入'abh'时,它现在应该搜索%abh%,而它搜索%ab%(在前一个请求中应该是这种情况).

在此输入图像描述

我想不出任何其他方式来解释这一点.我在这里错过了什么?

ɹɐq*_*ɹǝɟ 5

使用keyup()而不是keydown

keydown
Fires when the user depresses a key. It repeats while the user keeps the key depressed.

keyup
Fires when the user releases a key, after the default action of that key has been performed.
Run Code Online (Sandbox Code Playgroud)