我正在寻找一个在input[type="text"]值改变后立即执行函数的(jQuery)事件处理程序。.change jQuery事件处理程序在input[type="text"]失去焦点后执行函数(这可能适用于另一种情况)。
keypress / up / down仅当用户在输入中键入内容时才起作用(而不是当我动态操作它时)。
对此有什么解决方案吗?
发布于 2011-05-13 03:20:17
首先,我会修改JQuery change事件,使其即使在使用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();
}
}
}如果您需要在其他时间触发它,请按照jimbojw提到的过程添加这些事件侦听器。
发布于 2011-05-13 03:10:39
在这种情况下,我要做的一件事就是将同一个处理程序绑定到一堆事件上。
$('input').bind("change blur keyup mouseup", function() {
$(this).css("color", "red"); // or whatever you want to happen
});请参阅http://api.jquery.com/bind/
https://stackoverflow.com/questions/5983039
复制相似问题