问题
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