keydown事件不适用于Chrome [英] keydown event not working in Chrome

查看:149
本文介绍了keydown事件不适用于Chrome的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有两个文本框和一个按钮的asp.net表单。

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

这适用于IE和Firefox,但不适用于Chrome。我使用开发人员工具在Chrome中调试JS,发现如果block被执行但是click事件没有被解雇,keycode内部的语句== 13。任何想法解决这个问题?

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?

推荐答案

它适用于我。

我使用JQuery 1.7.2和Google Chrome 18.0.1025.168(官方版本134367)

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

我也使用.kplay而不是#kplay进行了检查,结果没有问题。

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

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

这篇关于keydown事件不适用于Chrome的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆