英文:
Esc key to press a button: works if an alert() is fired in the function; otherwise Esc has to be pressed multiple times
问题
在ColdFusion应用程序中,每个页面上都有一个退出按钮。有时退出按钮会触发应用程序中的一些清理操作,而有时,当不需要清理时,它只是加载应用程序的前一个调用页面。我的客户要求他能够按Esc键返回(我已经告诉他不要使用浏览器中的后退箭头,因为这样当需要清理时就不会发生清理。因此,我不能侦听Esc然后使用history.back)。我的解决方案是侦听Esc键,然后让jQuery按页面上已存在的退出按钮。我通过将Exit按钮添加一个名为"exit"的类,并将以下脚本放在页面的头部来实现这一点:
<script>
$(function() {
document.addEventListener("keydown", function(event) {
const key = event.key;
if (key === "Escape") {
pressExitKey();
}
})
})
function pressExitKey() {
alert('再按一次Esc键退出');
$(document).find('input.button.exit').click();
}
</script>
按照目前的脚本编写,它是有效的。当然,要触发退出按钮,需要按两次Esc键(一次关闭警告窗口,再次触发退出按钮)。但这里有一点我不理解的地方。我之所以首次加入警告是因为如果不加入警告,需要按下Esc键的次数似乎是不可预测的,直到事件触发并按下退出按钮之前。如果按下次数有规律,我尚未能够确定。
通过在脚本中注释掉警告语句,可以重现这种效果。
至少保留警告语句会产生一致的结果。我可以轻松地告诉客户:“您可以按退出按钮,或者按两次Esc键。”这总比告诉他“一遍又一遍地按Esc键直到退出发生”要好。无论哪种方式,都看起来或感觉很不够完美(警告窗口弹出是一个眼的刺,不断按下不可预测次数的Esc键也很烦人)。
编辑:
我的问题是:为什么去掉警告会导致用户不断按下Esc键(次数不可预测且不一致),直到事件触发?
英文:
In a ColdFusion application, I have an Exit button on each page. Sometimes the exit button invokes some cleanup in the app and sometimes, when cleanup isn't necessary, it just loads the previous calling page of the app. My customer requested that he be able to hit the Esc key to go back. (I've told him NOT to use the back arrow in the browser because then the cleanup doesn't happen when it is necessary. Hence, I can't listen for Esc and then use history.back). My solution was to listen for the Esc key and then get jquery to press the Exit button already on the page. I did this by adding a class of "exit" to the Exit button, and putting the following script in the head section of the page:
<script>
$(function() {
document.addEventListener("keydown", function(event) {
const key = event.key;
if (key === "Escape") {
pressExitKey();
}
})
})
function pressExitKey() {
alert('Press Esc Again to Exit');
$(document).find('input.button.exit').click();
}
</script>
The script as it is written works. Of course, it takes pressing Esc TWICE to fire the Exit button. (Once to close the alert window, and again to fire the exit button. But here is what I don't understand. The only reason I put the alert in there in the first place is that without it, the Esc button has to be hit a seemingly unpredictable amount of times before the event fires and the Exit button is pressed. If there is a pattern to the number of times it has to be pressed, I have not been able to determine it.
The effect can be reproduced by commenting out the alert statement in the script.
Leaving the alert in there at least produces a consistent result. I easily can tell the customer, "You can either press the Exit button, or hit Esc twice." That's better than telling him to "hit the Esc button again and again until the Exit happens." Either way, it looks or feels sloppy. (It's an eyesore for the alert to popup, and it's annoying to press Esc over and over an unpredictable number of times.)
Edited:
My question is: why does leaving out the alert cause the user to press Esc over and over (an unpredictable and inconsistent number of times) before the event fires?
答案1
得分: 0
I found something that works, using Alt + x or Alt + X as the triggering keystroke instead of Esc. I do have a question about this. I'll put it at the bottom after the code. Credit for this idea goes to @BrockAdams. Many thanks.
document.addEventListener("keydown", function (zEvent) {
if (zEvent.altKey && (zEvent.key === "x" || zEvent.key === "X")) { // not case sensitive
pressExitButton();
}
})
function pressExitButton() {
$(document).find('input.button.exit').click();
}
The question: If I include this script in the application.cfm
template, the script will be included at the top of every page in the application. The question is, will adding the listener at the top of every page stack up somehow and eat memory or cause some conflict? If so, how would I prevent this?
英文:
I found something that works, using <kbd>Alt</kbd> + <kbd>x</kbd> or <kbd>Alt</kbd> + <kbd>X</kbd> as the triggering keystroke instead of <kbd>Esc</kbd>. I do have a question about this. I'll put it at the bottom after the code. Credit for this idea goes to @BrockAdams. Many thanks.
document.addEventListener ("keydown", function (zEvent) {
if (zEvent.altKey && (zEvent.key === "x" || zEvent.key === "X")) { // not case sensitive
pressExitButton();
}
})
function pressExitButton() {
$(document).find('input.button.exit').click();
}
The question: If I include this script in the application.cfm
template, the script will be included at the top of every page in the application. The question is, will adding the listener at the top of every page stack up somehow and eat memory or cause some conflict? If so, how would I prevent this?
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论