【问题标题】:keydown event not working in Chromekeydown 事件在 Chrome 中不起作用
【发布时间】:2011-10-04 08:07:46
【问题描述】:

我有一个带有两个文本框和一个按钮的 asp.net 表单。我已经实现了在输入按键时触发按钮的单击事件的代码。

<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();
        }
    });

这在 IE 和 Firefox 中运行良好,但在 Chrome 中却不行。我使用开发人员工具在 Chrome 中调试了 JS,发现 keycode == 13 if 块中的语句被执行,但点击事件没有以某种方式被触发。有什么办法解决这个问题吗?

【问题讨论】:

    标签: jquery asp.net google-chrome keydown


    【解决方案1】:

    它对我来说很好用。

    我使用的是 JQuery 1.7.2 和 Google Chrome 18.0.1025.168(官方版本 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');
    });
    

    我还检查了 .kplay 而不是 #kplay,并且没有问题。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-16
      • 1970-01-01
      相关资源
      最近更新 更多