gpt4 book ai didi

javascript - 为什么我的 JQuery UI 对话框不注册 keydown 事件?

转载 作者:行者123 更新时间:2023-12-01 01:46:47 25 4
gpt4 key购买 nike

以下答案如 this one ,我想将回车键卡在 JQuery 对话框上,这样我就可以触发与单击“确定”时相同的事件,但是该事件永远不会被调用,因此我永远不会收到“它有效”的警报:

 $("#logout_popup").dialog(
{
title: "Log Out",
width: 'auto',
height: 'auto',
modal: true,
draggable: false,
resizable: false,
open: function(e) {
$('.ui-widget-overlay').hide().fadeIn(600);

//This is the event that's never called
$(e.target).keydown(function(ev) {
alert("Worked!");
});
},
show: {
effect: 'fade',
duration: 600
},
hide: {
effect: 'fade',
duration: 600
},
buttons: [
{
text: "Yes",
click: logout
},
{
text: "No",
click: function() {
$('#logout_popup').dialog('close');
}
}
],
close: clear_forms
});

大多数对话框设置都是无关紧要的,但我将它们全部包含在内以防万一。为什么该事件从未被调用?

我应该补充一点,如果我使用事件 $("#logout_popup").keydown,它也不起作用,但如果我使用 $(document).keydown,它确实有效(尽管我不想过滤文档中的每个事件。

最佳答案

这将是至少触发警报并捕获 keydown 的事件(在某些元素上...)的方法

open: function() {
$('.ui-widget-overlay').hide().fadeIn(600);
$('theElementYouWant').keydown(function(e) {
alert("Worked!");
});
},

如果你想从下向上传递keydown事件,你应该尝试

$("#logout_popup").keydown(function(e) {
$(this).dialog({
title: "Log Out",
width: 'auto',
height: 'auto',
modal: true,
draggable: false,
resizable: false,
open: function() {
$('.ui-widget-overlay').hide().fadeIn(600);
if ( e.which == 13 )
alert('Hurrah!');
},

您可以使用 call(this,event) 传递事件,但我不知道您想要的时间和地点。
从这里:http://forum.jquery.com/topic/how-to-pass-event-target-to-a-function (@KevinB)。
电话:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/call .

关于javascript - 为什么我的 JQuery UI 对话框不注册 keydown 事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21655627/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com