使用 jquery 检测按下 Enter 键
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5631589/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Detect Enter key is pressed with jquery
提问by Awais Qarni
I have a text field which is showing a value. What I want is when the user writes a new value in that text field and presses enter, an ajax function is triggered to do a pagination operation. I have a text field like this:
我有一个显示值的文本字段。我想要的是当用户在该文本字段中写入一个新值并按下 Enter 键时,会触发一个 ajax 函数来执行分页操作。我有一个这样的文本字段:
<input type="text" id="page" name="page" value="<?php echo($this->pn);?> />
And when a user writes any new value and presses Enter, I want the following ajax function triggered:
当用户写入任何新值并按 Enter 键时,我希望触发以下 ajax 函数:
update_ajax2({rpp:<?php echo($this->rpp);?>,pn:document.page.paged.value,filter:'<?php echo($this->filter);?>',orderby:'<?php echo($this->orderby);?>'});
I tried using the keypress event to detect if(e.which===13)
but this doesn't solve the problem. Can anyone guide me?
我尝试使用 keypress 事件进行检测,if(e.which===13)
但这并不能解决问题。任何人都可以指导我吗?
回答by Santosh Linkha
<input type="text" id="txt"/>
$('#txt').keydown(function (e){
if(e.keyCode == 13){
alert('you pressed enter ^_^');
}
})
On jsfiddle.
回答by vcg
Use this, it works for me :)
使用这个,它对我有用:)
$("#search-txt" ).on( "keydown", function(event) {
if(event.which == 13)
return false;//do something
});
回答by entropo
Check out this plugin: https://github.com/jeresig/jquery.hotkeys
回答by Muhammad Ummar
Key pressed event should work try it
按键事件应该可以尝试
function keyPressedTest(a)
{
a=(a)?a:window.event;
input=(a.target)?a.target:a.srcElement;
if(a.keyCode==13)
{
your_function()
}
}