keydown event not working in Chrome

我是研究僧i 提交于 2019-12-11 18:17:14

问题


I have an asp.net form with two textboxes and one button. I have implemented the code to fire the click event of the button on enter key press.

<asp:LinkButton ID="statementSearchButton" runat="server" OnClick="GetSearchResults" class="enterButton">
</asp:LinkButton>

$(document).keydown(function (event) {
        if (event == undefined) {
            event = window.event;
        }
        if (event.keyCode == 13) {
            $('.enterButton').focus();
            $('.enterButton').click();
        }
    });

This works fine in IE and Firefox but not in Chrome. I debugged the JS in Chrome using Developer tools and found that the statements inside the keycode == 13 if block are executed but the click event is not getting fired somehow. Any idea to fix this problem?


回答1:


It works fine for me.

I'm using JQuery 1.7.2, and Google Chrome 18.0.1025.168 (Official Build 134367)

$(document).keydown(function (event) {
    if (event == undefined) {
        event = window.event;
    }
    if (event.keyCode == 13) {
        alert("keyCode 13");
        $('#kplay').focus();
        $('#kplay').click();
    }
 });

$("#kplay").click(function(event) {
   alert('#kplay clicked');
});

I also checked with .kplay instead of #kplay, and that worked without issue.

$(document).keydown(function(event){
    if (event.which == 13) $('.kplay').click();
});


来源:https://stackoverflow.com/questions/7644896/keydown-event-not-working-in-chrome

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!