文本字段输入更改时触发延迟更改事件 [英] Fire deferred change event when text field input changes

查看:86
本文介绍了文本字段输入更改时触发延迟更改事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据 jQuery API


更改事件在其值更改时发送到元素。此活动仅限于< input> 元素,< textarea> 框和< ;选择> 元素。对于选择框,复选框和单选按钮,当用户使用鼠标进行选择时会立即触发事件,但对于其他元素类型,事件将延迟到元素失去焦点。

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.

当输入文本发生变化时,有没有办法让文本字段和文本区域触发事件,而不仅仅是当它们失去焦点时?

Is there a way to make the text fields and text areas fire an event when the input text is changed and not only when they lose focus?

推荐答案

您正在寻找html5 输入事件

You're looking for the html5 input event:

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

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

However, it doesn't work in older browsers. You can hook on multiple events (most relevant keyup), and fire your listener on each of them. If you're doing rather heavy computations, you should filter to those events only that led to an actual change:

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

这篇关于文本字段输入更改时触发延迟更改事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆