jQuery UI - 使用onkeypress Enter / Space打开时,对话框立即关闭 [英] jQuery UI - Dialog closes immediately when opened with onkeypress Enter/Space

查看:151
本文介绍了jQuery UI - 使用onkeypress Enter / Space打开时,对话框立即关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户在给定元素上按Enter或空格时,我想打开一个jQuery UI对话框。看来输入/空格键正在被对话框处理,但是会导致默认元素(取消按钮)被按下。该对话框几乎在打开时立即关闭。

I want to open a jQuery UI dialog when a user presses enter or space on a given element. It appears that the enter/space keys are being processed by the dialog, however, causing the default element (the cancel button) to be pressed. The dialog is closed almost as soon as it opens.

这是一个简化的演示:


<script type="text/javascript">
    function Go() {
        $("#dialog").text("Are you sure?").dialog({
            modal: true,
            buttons: {
                Cancel: function() {
                    $("#dialog").dialog("destroy");
                },
                OK: function() {
                    $("#dialog").dialog("destroy");
                }
            }
        });  // end .dialog
    }
</script>

<input type="button" value="Test" onkeydown="Go()" />
<div title="Dialog" style="display: none;" id="dialog"></div>

如果按钮被赋予焦点,然后用户按空格,对话框将打开,然后立即关闭。如果用户按Enter键,对话框会很快关闭,甚至不会闪烁(至少这是我在Firefox 3.5.3中的体验)

If the button is given focus and then the user presses space the dialog opens and then immediately closes. If the user presses enter, the dialog closes so quickly it doesn't even flash (at least that was my experience with Firefox 3.5.3)

如何防止对话框处理来自onkeydown事件的键,导致对话框打开?

How do I prevent the dialog from processing the key from the onkeydown event which caused the dialog to open?

推荐答案

尝试onkeyup如果有一个。如果它有效,我会给你解释(繁忙)。

Try onkeyup if there is one. If it works, I'll give you the explanation(Busy).

编辑Explantion:
主要是因为onkeydown事件没有被调用一次被按下时不断呼叫,而且由于电脑的运行速度如此之快(1秒是永恒的),所以在一次按下之前多次打电话。解决方案不是在被推下来时调用它,而是将其称为上一部分(当您放开时)。

Edit Explantion: It's mainly because the onkeydown event does not get called once, it gets called continually while it's pressed down, and since computers opperate so fast(1 second is eternity to them) it get's called many times during one push down. The solution is not to call it when it's pushed down, but call it on the up part(when you let go).

在电子设备中发生开关和逻辑门等我忘了技术术语。

Happens all the time in electronics with switches and logic gates etc. I forgot the technical terms.

这篇关于jQuery UI - 使用onkeypress Enter / Space打开时,对话框立即关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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