使用jQuery触发按键 [英] Trigger Keypress with jQuery
问题描述
在>确定触发器Keypress jQuery线程上,没有有效的JSFiddle来寻找答案,并且那里的代码对我不起作用.
On the Definitive Trigger Keypress jQuery thread there is no working JSFiddle for the answer, and the code that is there doesn't work for me.
$("button").click(function () {
$("input").focus();
var e = jQuery.Event("keydown");
e.which = 77; // # Some key code value
$("input").trigger(e);
})
有我的代码,这是我的小提琴 http://jsfiddle.net/Z8adb/
There's my code and here's my fiddle http://jsfiddle.net/Z8adb/
单击时,在输入中应出现M,因为输入已获得焦点并具有按下其keyCode为77("m")的keydown.
On click, an M should appear in the input, as the input is given focus and having a keydown with the keyCode of 77 ("m") triggered on it.
有什么想法吗?
我的真正目的是在Sublime Video上触发"m"热键,以便以编程方式使视频静音.这是确保我正确触发"m"键的第一步,而这正是我借助Stack Overflow进行的.但是,我仍然无法在视频上以编程方式触发事件.我认为这只是Sublime Video的问题,但我不确定,任何人对强迫按键和单击的看法都会令人赞叹.
My true purpose for this is to trigger an "m" hotkey on a Sublime Video in order to mute the video programmatically. This was my first step to ensure I was firing the "m" key properly, which I am with the help of Stack Overflow. However, I'm still not able to get an event to fire programmatically on the video. I think this is just a problem with Sublime Video, but I'm not sure, and anyone's views on forcing keypresses and clicks would be awesome to hear.
推荐答案
使用触发器,您只是通过键码触发事件,而没有将值分配给文本框.试试:- http://jsfiddle.net/PbHD2/
Using trigger you are just triggering the event with a keycode but not assigning the value to the textbox. Try this :- http://jsfiddle.net/PbHD2/
$("button").click(function() {
$("input").focus();
var e = jQuery.Event("keydown");
e.which = 77; // # Some key code value
$("input").val(String.fromCharCode(e.which));
$("input").trigger(e);
});
$('input').keydown(function(e){
console.log('Yes keydown triggered. ' + e.which)
});
这篇关于使用jQuery触发按键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!