如何处理ENTER按键以触发onBlur()事件? [英] How to handle the ENTER keypressed to trigger an onBlur() event?
问题描述
我有一个iPad webapp,在某些时候有一个很大的< form>
。其中的每个输入都有控制值的功能,包括keyUp和Blur事件。
I have an iPad webapp with a big <form>
at some point. Every input in it has functions controlling the values, both on keyUp and Blur events.
事情是,如果用户在键入时意外点击GO按钮(被认为是一个输入按键),表单被提交。我想拦截这个comportment并触发focus元素的onBlur()事件。
Thing is, if the user accidentaly hits the "GO" button while typing (considered as an "enter" keypress), the form is submitted. I would like to intercept this comportment and trigger the onBlur() event of the focused element instead.
现在我有这个:
load(){
document.addEventListener("keydown",logPressedKeys,false);
}
/**
* logs the keys hit in the console, will eventually trigger my onBlur event
*/
function logPressedKeys(e) {
console.log(e.keyCode);
if (e.keyCode==13) {
console.log('Enter spotted: prevent!');
e.preventDefault();//Shall prevent submitting
return false;//Hoping it prevents default if above fails
}
}
你们对此有什么建议/想法/改进吗?
Do you guys have any advice/idea/improvement about that?
Nota bene :必须至少有一个输入专注于iPad的键盘弹出。
Nota bene: There has to be at least one input focused for the iPad's keyboard to pop.
推荐答案
发现document.activeElement工作了在iPad的Safari中。
Found out that document.activeElement works out in iPad's Safari.
function logPressedKeys(e) {
console.log(e.keyCode);
if (e.keyCode==13) {
e.preventDefault();
console.log('Enter spotted: prevent!');
temp=document.activeElement;
//console.log(temp);
temp.onblur();
return false;
}
return true;
}
这篇关于如何处理ENTER按键以触发onBlur()事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!