检测输入类型文本的程序化更改 [英] Detect programmatic changes on input type text

查看:90
本文介绍了检测输入类型文本的程序化更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有
$ b $有一种方法可以在脚本更改输入类型文本的值时获得通知。 b

 < input id ='test'type ='text'/> 

和脚本

  document.getElementById('test')。value ='bbb'; 

我希望收到文字变更的通知。



我知道我可以通过keydown,keyup,onblur etcetera(如果我试图跟踪用户输入时工作)通知,但如果更改是以编程方式完成的话,那么该怎么办?



非常感谢

ps
没有JQuery请求,或者jquery是否可以解释它是如何实现它的。

解决方案

使用现代浏览器,您可以尝试如下所示:

  var input = document.getElementById('test'); 
input._value = input.value;
Object.defineProperty(input,value,{
get:function(){return this._value;},
set:function(v){
// Do你的东西
this._value = v;
}
});

如果您不希望任何用户输入(即隐藏类型输入字段),因为它对DOM基本功能非常具有破坏性。记住这一点。


Is there a way to get informed when a script changes the value of an input type text.

I have

<input id='test' type='text' />

and a script

document.getElementById('test').value = 'bbb'; 

I want to be notified of the change of the text.

I know I could be notified by keydown,keyup, onblur etcetera (which works if I am trying to track user typing) but what about if the change is done programmatically ?

Many thanks

p.s. No JQuery please or if jquery does it can somebody explain how it achieves it.

解决方案

If you're dealing with a modern browser, you can try with something like this:

var input = document.getElementById('test');
input._value = input.value;
Object.defineProperty(input, "value", {
    get: function() {return this._value;},
    set: function(v) {
        // Do your stuff
        this._value = v;
    }
});

This solution is good if you actually don't expect any user input (i.e., hidden type input fields), because it's extremely destructive of the DOM basic functionality. Keep that in mind.

这篇关于检测输入类型文本的程序化更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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