jQuery-当输入字段为焦点时,具有Enter键的Target按钮 [英] jQuery - Target button with enter key when input field is focused

查看:47
本文介绍了jQuery-当输入字段为焦点时,具有Enter键的Target按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个输入字段和一个登录按钮.我想允许用户在输入字段为焦点时按下输入"键以单击按钮.我将如何使用jQuery?

I have an input field and a login button. I want to allow users to hit the "enter" key to click the button when the input field is focused. How would I do this with jQuery?

<input type="text" size="10" id="loginUserID" name="username">
<input type="button" value="Login" name="loginBtn" id="loginBtn">

推荐答案

$('#loginUserID').keypress(function(event){
  if(event.keyCode == 13){
    $('#loginBtn').click();
  }
});

演示: http://jsfiddle.net/Bhf5a/

这篇关于jQuery-当输入字段为焦点时,具有Enter键的Target按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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