如何使用jQuery触发输入事件 [英] How to trigger enter event with jquery
本文介绍了如何使用jQuery触发输入事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下可行的方法:
$('textarea').keypress(function(e) {
if (e.keyCode == '13') {
alert('code');
}
});
但是我想在页面加载时触发同样的事情,我尝试了以下操作,但不起作用:
But I want to trigger that same thing when the page loads, I tried the following but it doesn't work:
var e = jQuery.Event("keypress");
e.which = 13; // Enter
$('textarea').trigger(e);
注意:我想在这里删除第一段代码,我不想删除它.
NOTE: I want to have the first snipped of code there, I DO NOT want to remove it.
推荐答案
使用"which"代替keyCode. 哪个"在两种情况下都有效
Use "which" instead of keyCode. "Which" works in both scenario
$('textarea').keypress(function (e) {
if (e.which == '13') {
alert('code');
}
});
这篇关于如何使用jQuery触发输入事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文