按下Enter键时运行Jquery方法 [英] Run Jquery method when enter key is pressed

查看:118
本文介绍了按下Enter键时运行Jquery方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在按下回车键时运行一种方法.按钮已经使用了此方法.基本上,当用户填写文本字段并单击提交"按钮时,他们会得到一个灯箱.我要执行的操作是执行相同的操作,但是要按下Enter键.香港专业教育学院研究使用.keypress,但我只能找到表单标签是标准html表单标签而不是asp.net表单标签的示例.我认为目前该窗体仅响应页面上的.net控件,并且我无法使用jquery方法覆盖它.有什么想法吗?

I'm trying to run a method when the enter key has been pressed. This method is already being used by a button. Basically, when the user fills in a text field and clicks a submit button they get a lightbox. What I want to do is run this same action, but when the enter key has been pressed. Ive looked into using .keypress but I can only find examples where the form tag is a standard html form tag and not a asp.net form tag. I think at the moment the form is responding only to the .net controls on the page and I cant override this with the jquery method. Any ideas?

推荐答案

您可以将keypress事件绑定到文档,将trigger()事件绑定到按钮上的处理程序.

You can bind a keypress event to the document and trigger() the handler on your button.

$(document).bind('keypress', function(e){
   if(e.which === 13) { // return
      $('#buttonid').trigger('click');
   }
});

只要您在站点上按返回键,就会触发该事件.您可能只希望某人在您的输入表单中使用return时才发生.您只需要将selectordocument更改为与您的控件匹配的任何一种选择器即可.

This would fire whenever you press the return key on your site. You probably want it only to happen if someone uses return in your input form. You would just need to change the selector from document to any kind of selector that matches your control.

引用: .trigger()

这篇关于按下Enter键时运行Jquery方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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