神秘的鼠标事件关闭了jQuery UI对话框
这显然是一个SSCCE 。
所以我们的任务是编写导弹发射控制系统的前端。 我们选择Spartan布局,因为这是非常严重的:只需一个文本输入框和一个输入代码的按钮:
为安全起见,点击“确定”按钮后,我们将显示一个对话框,要求用户确认:
作为可用性完成触摸,我们为Enter按钮添加一个键监听器,这也将导致单击“确定”按钮(使用$.trigger()
)。
不幸的是,确认对话框仅在用户单击“确定”按钮时显示,但在按Enter键时不显示。 当我们按Enter键时 ,对话框根本不显示。
最糟糕的是,在添加一些调试消息后,似乎对话框确实显示了几分之一毫秒,然后由于某种原因点击了“Yeap”按钮。 因此,当Enter被击中时,导弹发射立即被确认!
在这里小提琴。
代码如下:
function inputKeyListener(evt) { console.log('key listener - triggered key code is: ' + evt.keyCode); if (evt.keyCode === $.ui.keyCode.ENTER) { evt.stopPropagation(); $('#missile-launch-button').click(); // Directly calling confirm() doesn't work either } } function missileLaunchButtonClickHandler(e) { e.stopPropagation(); confirm(); } function confirm() { var launchCode = $('#missile-launch-code-input').val(); const dialog = $('#missile-launch-confirmation-modal'); dialog.dialog({ closeOnEscape: false, dialogClass: 'no-close', open: function(event, ui) { console.log('confirm :: open is called'); }, close: function() { console.log('confirm :: close is called'); }, resizable: false, height: "auto", width: 400, modal: true, buttons: { "Yeap": function() { console.log('Confirmation button was clicked'); $(this).dialog("close"); console.log('missile launch with code [' + launchCode + '] was confirmed!'); }, "Maybe not just yet": function(ev) { console.log('Abort button was clicked'); $(this).dialog("close"); console.log('Armageddon was averted'); } } }); dialog.dialog('open'); console.log('by this time the dialog should be displayed'); } $('#missile-launch-confirmation-modal').dialog({ autoOpen: false }); $('#missile-launch-button').click(missileLaunchButtonClickHandler); $(document).on('keydown', inputKeyListener);
<div id='missile-launch-confirmation-modal' title='Confirm missile launch'