.blur()事件中的截取值更改 [英] Intercept value changes within .blur() event

查看:181
本文介绍了.blur()事件中的截取值更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 .blur()函数在每次文本字段失去焦点时执行一些代码(也是当其值不更改时)。

I'm using .blur() function to execute some code every time a text field loses focus (also when its value doesn't change).

现在我需要添加一些仅在文本字段值更改时才执行的逻辑。有没有办法结合 .change()事件与 .blur()?或者更好的是,有没有办法知道我的文本字段中的值是否只是使用 .blur()

Now I need to add some logic that must be executed only when text field value changes. Is there a way to combine .change() event with .blur()? Or, better, is there a way to know if the value in my text field is changed just using .blur()?

推荐答案

不直接,但您可以将价值存储在焦点事件..

Not directly, but you can store the value on focus event..

$('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
        }
    });






当然,你可以为每一个绑定不同的处理程序事件..


Of course you could just bind different handlers for each event..

$('input')
   .on('change', function(){/*your change code*/})
   .on('blur', function(){/*your blur code*/});

这篇关于.blur()事件中的截取值更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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