如何在不按任何键的情况下通过Jquery调用按键事件

How to call a keypress event by Jquery without pressing any keys

本文关键字:Jquery 调用 事件 任何键 情况下      更新时间:2023-09-26

我想通过Jquery实现一个在输入元素中键入字符串的keypress事件,我知道Jquery可以监听keypress、keydown和keyup的事件。但是,我想做的是使用Jquery来实现按键的操作,并将我按下的键的值放入输入元素中。jQuery有可能实现这项任务吗?

这就是您想要的吗?

工作FIDDLE

$('.input').on('keypress', function(event) {
  event.preventDefault();
  $(this).val(event.keyCode);
});
$( ".input" ).trigger( "keypress" );
// Target input element and bind to the "keyPress" event.
$('.input').on('keypress', function(event) {
  // Prevent the default action to stop the key character being entered into the field
  event.preventDefault();
  // Add the event's keyCode into the field
  $(this).val(event.keyCode);
})

http://codepen.io/anon/pen/XXNOQd?editors=101