简体   繁体   English

在Textbox KeyDown事件上设置jQuery自动完成功能

[英]Set jQuery Autocomplete on Textbox KeyDown Event

I want jQuery Autocomplete on textbox key down event 我想在文本框按下事件上使用jQuery自动完成功能

I have tried below code, but it is not working. 我已经尝试过下面的代码,但是它不起作用。

$("#txtName").keyDown({
    $("#txtName").autocomplete({
        source: '@Url.Action("GetAllName", "Home")',
        select: function (event, ui) {
            $("#txtName").val(ui.item.value);
            return false;
        }
    });
});

You do not need the keyDown event otherwise you are rebinding the autocomplete on every keydown. 您不需要keyDown事件,否则,您将在每个keydown上重新绑定自动完成功能。

$("#txtName").autocomplete({
    source: '@Url.Action("GetAllName", "Home")',
    select: function (event, ui) {
        $("#txtName").val(ui.item.value);
        return false;
    }
});

To show all results when one character is entered add minLength: 1 要显示输入一个字符时的所有结果,请添加minLength: 1

If what you want is to have all the items displayed when the textbox has focus then this is what you are looking for: https://stackoverflow.com/a/4604300/1398425 如果您想要的是在文本框具有焦点时显示所有项目,那么您正在寻找的是: https : //stackoverflow.com/a/4604300/1398425

If you just want it to begin searching on the first keystroke, try using minLength: 1 : 如果只希望它在第一个按键上开始搜索,请尝试使用minLength: 1

$("#txtName").autocomplete({
    source: '@Url.Action("GetAllName", "Home")',
    minLength: 1,
    select: function (event, ui) {
        $("#txtName").val(ui.item.value);
        return false;
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM