admin管理员组

文章数量:1419202

I want jQuery Autoplete on textbox key down event

I have tried below code, but it is not working.

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

I want jQuery Autoplete on textbox key down event

I have tried below code, but it is not working.

$("#txtName").keyDown({
    $("#txtName").autoplete({
        source: '@Url.Action("GetAllName", "Home")',
        select: function (event, ui) {
            $("#txtName").val(ui.item.value);
            return false;
        }
    });
});
Share Improve this question edited Sep 15, 2014 at 11:09 Ashley Medway 7,3097 gold badges53 silver badges73 bronze badges asked Sep 15, 2014 at 11:05 User5590User5590 1,4356 gold badges28 silver badges68 bronze badges 6
  • i think $("#txtName").autoplete() is sufficient no need for keyDown – Sandip Pingle Commented Sep 15, 2014 at 11:08
  • What does the MVC action look like? – Ashley Medway Commented Sep 15, 2014 at 11:08
  • You just want it to begin searching on the first keystroke? Try using minLength: 1. – DevlshOne Commented Sep 15, 2014 at 11:08
  • @SandipPingle $("#txtName").autoplete() is working. But i want functionality like, when i first click on textbox and press key down, then all the results should e – User5590 Commented Sep 15, 2014 at 11:10
  • @AshleyMedway Here i am using Url.Action. so Home is my controller and GetAllName is ActionName – User5590 Commented Sep 15, 2014 at 11:11
 |  Show 1 more ment

2 Answers 2

Reset to default 4

You do not need the keyDown event otherwise you are rebinding the autoplete on every keydown.

$("#txtName").autoplete({
    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

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./a/4604300/1398425

If you just want it to begin searching on the first keystroke, try using minLength: 1:

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

本文标签: javascriptSet jQuery Autocomplete on Textbox KeyDown EventStack Overflow