0

I have an autocomplete textbox that cannot fire the key down event.

Here is the code for the auto complete box

<script type="text/javascript">
$(document).ready(function () {
    $("#TransFinishedPartNumber").autocomplete({
        source: function (request, response) {
            // define a function to call your Action 
            $.ajax({
                url: '@Url.Action("partLookUp", "Transaction")',

                // term will be the param used by your action method
                data: { term: request.term },
                success: function (data) {
                    response($.map(data, function (item) {
                        return { label: item.pt_part + " " + item.pt_desc1, value: item.pt_part };
                    }))
                    $("#TransFinishedPartNumber").removeClass('acLoading');
                },
                error: function () {
                    $("#TransFinishedPartNumber").removeClass('acLoading');
                }
            })
        },
        minLength: 1, // require at least one character from the user
        search: function () { $(this).addClass('acLoading'); },
    });

});

Now here is the event javascript it will never fire... not live or bind or anything for that matter... but the crazy thing is the autocomplete box works...

<script type="text/javascript">
//Transaction Event handlers and ajax calls
$(document).ready(function () {
    $("#TransFinishedPartNumber").bind("keydown",function (e) {
        $("#TransFinishedPartNumber").addClass("ui-state-default");

    });
    $("#TransFinishedPartNumber").bind("keydown",function (e) {
        $("#TransFinishedPartNumber").removeClass("ui-state-default");

    });
});

I tried this way also

<script type="text/javascript">
//Transaction Event handlers and ajax calls
$(document).ready(function () {
    $("#TransFinishedPartNumber").keydown(function (e) {
        $("#TransFinishedPartNumber").addClass("ui-state-default");

    });
    $("#TransFinishedPartNumber").keydown(function (e) {
        $("#TransFinishedPartNumber").removeClass("ui-state-default");

    });
});

I changed to this script to be absolutely sure

<script type="text/javascript">
//Transaction Event handlers and ajax calls
$(document).ready(function () {
    $("#TransFinishedPartNumber").keydown(function (e) {
        alert(e.which);
    });
});

4

1 回答 1

0

按住键时会不断触发 Keydown 事件。您可能想尝试使用 keyup。

例如,如果您执行以下操作并按住某些东西,它会一直持续下去。实际上每秒很多次:

$("body").bind("keydown",function(){
   $("body").append("pizza<br>");
});

但是keyup只发生一次!

$("body").bind("keyup",function(){
   $("body").append("pizza<br>");
});

因此,由于您的代码有两次 keydown 事件。它每秒多次添加和删除类。

于 2012-07-24T22:26:19.727 回答