我正在使用 .blur()
函数在每次文本字段失去焦点时(以及当它的值没有改变时)执行一些代码。
现在我需要添加一些仅在文本字段值更改时才必须执行的逻辑。有没有办法将 .change()
事件与 .blur()
结合起来?或者,更好的是,有没有办法知道我的文本字段中的值是否仅使用 .blur()
更改?
最佳答案
不是直接的,但是您可以将值存储在 focus
事件中..
有点像
$('input')
.on('focus',function(){
// store the value on focus
$(this).data('originalValue', this.value);
})
.on('blur',function(){
// retrieve the original value
var original = $(this).data('originalValue');
// and compare to the current one
if (original !== this.value){
// do what you want
}
});
当然你可以为每个事件绑定(bind)不同的处理程序..
$('input')
.on('change', function(){/*your change code*/})
.on('blur', function(){/*your blur code*/});
关于javascript - 拦截 .blur() 事件中的值变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16710227/