jQuery keydown事件上的trigger()和stopPropagation() [英] jQuery trigger() and stopPropagation() on keydown event
问题描述
我在按键和停止传播方面遇到困难
I am having a difficult time with keydown and stop propagation
我不希望刷新页面,我已经尝试了所有可以想到的方式,当前代码是
i do not want my page to refresh, i have tried every which way i can think of, my current code is
<script>
$(document).ready(function() {
var e = jQuery.event( 'keydown', { which: $.ui.keyCode.ENTER } );
$('#id_number').trigger(e, function(event){
event.preventDefault();
event.stopPropagation();
});
});
</script>
对我在这里做错的任何想法吗?我以为该事件已正确调用,我的jquery和jquery ui正确链接并且没有收到控制台错误
any idea on what i am doing wrong here? I thought that the event was called correctly, i have jquery and jquery ui linked correctly and receive no console errors
更新 很好,它正在工作,现在我正在 对象函数(a,b)的属性'event'{返回新的e.fn.init(a,b,h)}不是函数 以下代码错误
UPDATE well it was working, now im getting Property 'event' of object function (a,b){return new e.fn.init(a,b,h)} is not a function error on the below code
$(document).ready(function() {
var e = jQuery.event( 'keydown', { which: $.ui.keyCode.ENTER } , function(event){
event.preventDefault();
event.stopPropagation();
});
$('#id_number').trigger(e);
});
更新#2-已修复
我学到的东西
-
(准备就绪)文档中不需要
- trigger()
- 让$ .ui.keyCode正常工作是困难的(至少对我来说如此)
- 始终查看输入(onblur,onfocus等)又附加了哪些其他功能
- trigger() not needed in the document(ready)
- getting $.ui.keyCode to work was difficult (at least for me)
- always see what other functions are attached to the input (onblur, onfocus, ect) aka doh
将它改写成这个,效果很好
rewrote it to this, works perfectly fine
$(document).ready(function() {
$('#id_number').keydown(OnKeyDown);
});
function OnKeyDown(e){
var code = (e.keyCode ? e.keyCode : e.which); //to support both methods
if(code == 13) { //the Enter keycode
//my actions
return false;
}
}
推荐答案
停止默认操作的代码必须转到该事件所适用的元素.
The code to stop the default action must go to the element that the event applies to..
$('#id_number').keydown(function(event){
event.preventDefault();
event.stopPropagation();
});
.trigger()
的第二个参数用于extraParameters
这篇关于jQuery keydown事件上的trigger()和stopPropagation()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!