本文介绍了如何检测用户何时在输入字段中按Enter键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我只有1个输入字段,我想注册onChange和onKeyPress事件来检测用户何时完成输入或按Enter键。我只需要使用javascript。感谢您的帮助。
I only have 1 input field and I want to register onChange and onKeyPress event to detect when the users finish their input or press the Enter key. I need to use javascript only. Thanks for the help.
我有:
var load = function (){
//I want to trigger this function when user hit Enter key.
}
document.getElementById('co').onchange=load; //works great
document.getElementById('co').onKeyPress=load;
//not sure how to detect when user press Enter
html
//no form just a single input field
<input type='text' id='co'>
推荐答案
document.getElementById('foo').onkeypress = function(e){
if (!e) e = window.event;
var keyCode = e.keyCode || e.which;
if (keyCode == '13'){
// Enter pressed
return false;
}
}
这篇关于如何检测用户何时在输入字段中按Enter键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!