jQuery输入事件 - 触发器('change')不是它

时间:2021-06-24 20:29:51

I have the following event listener on my inputs on a HTML page:

我在HTML页面上输入了以下事件监听器:

$('input').on('input', function (e) {
    console.log(this.value);
});

I understand the input event captures the following individual events change, keyup and paste.

我理解输入事件捕获以下单个事件更改,加密和粘贴。

When I update an input value using jQuery, I want to trigger this event so my event code runs. If I run:

当我使用jQuery更新输入值时,我想触发此事件,以便我的事件代码运行。如果我跑:

//Does not work
$('input').val('test').trigger('change');

My event does not fire. I expected the input event handler to catch the change event. If I run:

我的事件不会发生。我期望输入事件处理程序捕获change事件。如果我跑:

//Does work
$('input').val('test').trigger('input');

This does work... why does triggering the change event not fire my input event handler?

这确实有效...为什么触发更改事件不会触发我的输入事件处理程序?

1 个解决方案

#1


I expected the input event handler to catch the change event.

我期望输入事件处理程序捕获change事件。

That's where you went wrong - the input event handler does not catch change events.

这就是你出错的地方 - 输入事件处理程序不会捕获更改事件。

#1


I expected the input event handler to catch the change event.

我期望输入事件处理程序捕获change事件。

That's where you went wrong - the input event handler does not catch change events.

这就是你出错的地方 - 输入事件处理程序不会捕获更改事件。