gpt4 book ai didi

javascript - 当文本字段输入更改时触发延迟更改事件

转载 作者:行者123 更新时间:2023-11-28 13:34:06 24 4
gpt4 key购买 nike

根据jQuery API :

The change event is sent to an element when its value changes. This event is limited to <input> elements, <textarea> boxes and <select> elements. For select boxes, checkboxes, and radio buttons, the event is fired immediately when the user makes a selection with the mouse, but for the other element types the event is deferred until the element loses focus.

有没有办法让文本字段和文本区域在输入文本更改时触发事件,而不仅仅是在失去焦点时触发事件?

最佳答案

您正在寻找 html5 input event :

$(…).on("input", function(e) {…});

但是,它在旧版浏览器中不起作用。您可以 Hook 多个事件(最相关的 keyup),并在每个事件上触发监听器。如果您正在进行相当繁重的计算,则应该仅过滤那些导致实际更改的事件:

var val;
$(…).on("input change keyup keypress cut paste mouseup focus blur", function(e) {
if (this.value !== val)
val = this.value;

}
});

关于javascript - 当文本字段输入更改时触发延迟更改事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22747806/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com