输入值以编程方式更改时触发更改事件?

输入值以编程方式更改时触发更改事件?

问题描述:

我的表单中有一个输入法。

I have an Input in my form.

<input type="text" id="changeProgramatic" onchange="return ChangeValue(this);"/>

如果我使用另一个JavaScript函数更改此textBox(changeProgramatic)中的值,它将不会触发改变事件。(注意:我将'this'传递给方法)

If I change the value in this textBox (changeProgramatic) using another JavaScript function it won't trigger the change Event.(Note: I'm passing 'this' into the method)

你正在使用jQuery,对吗?将JavaScript与HTML分开。

You are using jQuery, right? Separate JavaScript from HTML.

您可以使用触发器 triggerHandler

var $myInput = $('#changeProgramatic').on('change', ChangeValue);

var anotherFunction = function() {
  $myInput.val('Another value');
  $myInput.trigger('change');
};