Javascript:如何在不用 JQuery 的情况下以编程方式更改 <input> 后发送事件

此代码片段将以编程方式更改输入,然后分派 input 事件,该事件将告诉页面上运行的 javascript 代码输入已更改。

dispatch_input_event.js
let myinput = document.querySelector("input#myinput");
// 设置值
myinput.value = "newvalue";
// 模拟 input 事件
myinput.dispatchEvent(new Event('input', {
    bubbles: true,
    cancelable: true,
}));

Check out similar posts by category: Javascript