我有一个网站,有几个按钮可以打开不同的 jQuery ui 对话框。
其中一个对话框(ID 为 filter_story
)中有一个文本字段,用户可以在其中输入关键字,我稍后会在轮询服务器时使用这些关键字来过滤故事。我设置了文本字段,以便用户可以输入多个关键字,在输入每个项目后单击类 approve_filter_item
的按钮,或者用户只需单击 enter
每个关键字之间的按钮。
// button to open the keyword filter list
$( "#filter_stories" ).click(
function(){
$( "#filter_dialog" ).dialog( "open" );
}
);
$(".approve_filter_item").click(function(){
[process the just inserted keyword and then put focus back on the textfield]
});
//listen for enter key when user's focus is on entering a new keyword
$(".new_filter_item").keydown(function (e) {
if (e.keyCode == 13) {
$($(this).parents("tr")[0]).find(".approve_filter_item").click();
}
});
// button to show the post story dialog form
$( "#post_a_story" ).click(
function(){
$( "#new_story_form" ).dialog( "open" );
}
);
在所有版本的 Chrome、FF 和 Safari 中,一切都完全按照预期运行。然而,我在 IE 中遇到了一些不良行为。
假设 IE 用户在#filter_stories
对话框中输入关键字,然后单击enter
;所发生的情况是 keydown
事件监听器按预期触发,但在执行后 #post_a_story
的单击事件监听器也会触发。以前,我使用 keyup
而不是 keydown
,在这种情况下,当用户单击 时,
,我的 #post_a_story
点击事件会立即神秘地触发输入keyup
事件监听器根本没有触发。如果我完全删除 post_a_story
事件监听器,则会打开另一个对话框。为什么按 enter
键会触发不相关的 click
事件?
请注意,如果用户单击 approve_filter_item
按钮而不是单击 enter
,则将按预期处理关键字。
最佳答案
正如 @bfavaretto 指出的那样,.preventDefault() 结束了这种奇怪的 ie 行为。我不知道为什么会发生这种事。
关于javascript - 单击事件在 IE 中打开错误的对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16390457/