简体   繁体   English

更改事件未使用 blur() 方法触发

[英]change event not triggered with blur() method

When a user types something in an <input type="text"> and removes the focus, both blur and change are fired.当用户在<input type="text">并移除焦点时, blurchange被触发。 However, when I do the same thing in JavaScript using the blur() method, only the blur event is fired and not the change event.但是,当我使用blur()方法在 JavaScript 中做同样的事情时,只会触发blur事件而不是change事件。 Is there a reason for this inconsistency?这种不一致是否有原因?

See this example code ( jsfiddle ):请参阅此示例代码( jsfiddle ):

<input type="text">
<button>Test</button>
const input = document.querySelector("input")

input.addEventListener("blur", () => console.log("blur"))
input.addEventListener("change", () => console.log("change"))

const button = document.querySelector("button")
button.addEventListener("click", () => {
    setTimeout(() => {
        input.focus()
    
        input.value = "something"
    
        input.blur()
    }, 1000)
})

When clicking the button, after a second, it should focus the input, change the value and blur the input.单击按钮时,一秒钟后,它应该聚焦输入,更改值并模糊输入。 However, only the blur event is fired.但是,只有blur事件被触发。 Doing the same by hand will fire both events.手动执行相同的操作将触发这两个事件。

I like to trigger some validation logic on the change event and it works perfectly fine in real-live but when I try to recreate the workflow in a unittest it fails and I'm not sure why and how to solve it.我喜欢在change事件上触发一些验证逻辑,它在现实生活中工作得非常好,但是当我尝试在单元测试中重新创建工作流时,它失败了,我不确定为什么以及如何解决它。 So the alternative question is: How can I trigger the change event from JavaScript?所以另一个问题是:如何从 JavaScript 触发change事件?

You can trigger an event like this:您可以触发这样的事件:

const input = document.querySelector("input");
const event = new Event("change");

// blur the input
input.blur();

// dispatch change event manually
input.dispatchEvent(event);

https://developer.mozilla.org/en-US/docs/Web/Guide/Events/Creating_and_triggering_events https://developer.mozilla.org/en-US/docs/Web/Guide/Events/Creating_and_triggering_events

This issue这个问题

The change event is specifically emitted when a user interacts with an element.用户与元素交互时,会特别发出change事件。 This is built in a was intentional.这是有意为之。 @see HTMLElement: change event . @see HTMLElement:更改事件

The solution解决方案

Use synthetic events to mimic user interaction in changing the value: input.dispatchEvent(new Event("change"));使用合成事件来模拟用户在更改值时的交互: input.dispatchEvent(new Event("change"));

Below is a working example with the logic in its own function updateValueWithChangeEvent .下面是一个工作示例,其逻辑在其自己的函数updateValueWithChangeEvent

 const input = document.querySelector("input") input.addEventListener("blur", () => console.log("blur")) input.addEventListener("change", () => console.log("change")) // Updates the value of an input and triggers the change Event. const updateValueWithChangeEvent = (input, value) => { if (input.value === value) return input.value = value input.dispatchEvent(new Event("change")); } // Updated example using function above const button = document.querySelector("button") button.addEventListener("click", () => { setTimeout(() => { // This will update value and trigger change event updateValueWithChangeEvent(input, "something") // This will trigger the blur event input.focus() input.blur() }, 1000) })
 <input type="text"> <button>Test</button>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM