jQuery事件处理程序,用于立即更改输入

iWe*_*Web 6 javascript forms jquery onchange event-handling

我正在寻找一个(jQuery)事件处理程序,它在input[type="text"]值更改后立即执行一个函数.在.changejQuery的事件处理程序执行后的功能input[type="text"]失去焦点(这可能是很好的另一种情况).

keypress/ up/ down只有当用户键入的东西到输入(而不是当我在动态操控它)的工作.

这有什么解决方案吗?

jim*_*mbo 10

我在这种情况下做的一件事是将同一个处理程序绑定到一堆事件.

$('input').bind("change blur keyup mouseup", function() {
    $(this).css("color", "red"); // or whatever you want to happen
});
Run Code Online (Sandbox Code Playgroud)

http://api.jquery.com/bind/


Bri*_*y37 3

首先,我将修改 JQuery 更改事件,即使您使用该val函数动态更改它,也会触发该事件。我真的不明白为什么这不是 JQuery 中的默认行为。这是执行此操作的代码:

//Store the old val function
$.fn.custom_oldVal = $.fn.val;

//Update the val function to fire the change event where appropriate:
$.fn.val = function(value) {
    if(value == null || value == undefined){
        return this.custom_oldVal();
    } else {
        //Only call onchange if the value has changed
        if(value == this.custom_oldVal()){
            return this;//Return this object for chain processing
        } else {
            return this.custom_oldVal(value).change();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

如果您需要在其他时间触发它,请按照 jimbojw 提到的过程添加这些事件侦听器。