处理“输入” /“返回” Chrome中的关键 [英] Handling the "Enter" / "Return" key in Chrome

查看:59
本文介绍了处理“输入” /“返回” Chrome中的关键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个AJAX-y类型的页面。当用户点击GO时,我需要执行特定的javascript函数。当用户点击Enter或Return时,我也想模拟GO点击。请注意我不想提交页面。原因是因为我通过JavaScript做所有事情。

I have an AJAX-y type page. When the user clicks "GO" I need to execute a specific javascript function. I also want to simulate the "GO" click when a user hits "Enter" or "Return. Please note that I do not want to "submit" the page. The reason why is because i am doing everything via JavaScript.

要处理输入/返回键,请按IE和FireFox,我使用以下代码:

To handle the "Enter" / "Return" key press in IE and FireFox, I am using the following code:

$(document).ready(function () {  
  $('#root').keypress(function(e) { 
    if (e.keyCode == '13') { 
      goButton(); 
    } 
  });
});

不幸的是,这段代码在Chrome中无效。调用goButton()函数。但问题是页面被提交。我做错了什么?

Unfortunately, this code is not working in Chrome. The goButton() function gets called. The problem though is the page gets submitted. What am I doing wrong?

推荐答案

假设你有一张表格:

$('#theForm').submit(function(e){
    e.preventDefault();
    goButton();
    return false; // just to be sure.
});

你需要阻止从表单提交事件,当表单具有焦点并且用户按下回车时调用该事件。

You need to prevent the submit event from the form, which gets called when the form has focus and a user presses enter.

这篇关于处理“输入” /“返回” Chrome中的关键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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