在输入文本上触发jQuery的keypress事件 [英] Trigger the jQuery's keypress event on an input text
问题描述
关于 .trigger()
方法, which
属性, JS字符代码和下面的代码,为什么#example
输入没有获得字符a
作为自动编写的值?我误解了.trigger()
方法吗?
Regarding the .trigger()
method, the Event
object, the which
property, the JS char codes and the code below, why does the #example
input don't get the char a
as auto-written value? Did I misunderstand the .trigger()
method?
<input type="text" name="example" id="example" value="" />
<script>
$(function() {
var e = jQuery.Event("keydown", { which: 65 });
$("#example").focus().trigger(e);
});
</script>
推荐答案
您犯的错误是您期望jQuery的trigger
方法执行的操作.如果您查看代码,您将看到它在做什么实际上是在执行jQuery注册的事件处理程序,而不是不是 DOM Level 3事件.因为它只执行jQuery处理程序,所以您不会引起change
事件,这是触发更新文本框的value属性所需要的事件.
The mistake you're making is what you're expecting the jQuery's trigger
method to do. If you check out the code you'll see that what it is doing is actually executing the jQuery registered event handlers not the DOM Level 3 events. Because it's only executing jQuery handlers you wont be causing the change
event which is what you need to be triggered to update the value property of the textbox.
这篇关于在输入文本上触发jQuery的keypress事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!