在这样的答案之后,我想在 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
,它确实有效(尽管我宁愿不必过滤文档中的每个事件。
这至少是触发警报并捕获键控event
的方法(在某些元素上...
open: function() {
$('.ui-widget-overlay').hide().fadeIn(600);
$('theElementYouWant').keydown(function(e) {
alert("Worked!");
});
},
如果你想从下往上传递keydown event
,你应该尝试
$("#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。