如何检测从JavaScript文本框更改事件 [英] How to detect textbox change event from javascript

查看:98
本文介绍了如何检测从JavaScript文本框更改事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本框asp.net服务器控件。我修改从JavaScript我希望在文本框文本更改为文本框检测的价值。我想它被调用遗失FOUCS当文本改变onchange事件。但在我的情况下,我一个从Javascript更改文本。我怎样才能做到这一点?

I have a textbox asp.net server control. I am modifying the value of textbox from javascript i want to detect when the textbox text is changed. I tried onchange event which gets called on lost foucs when the text changes. But in my case i an changing text from Javascript. how can i achieve this?

推荐答案

更​​新属性的不会的触发更改事件。的变化事件是由用户触发的。

Updating the value property won't trigger the change event. The change event is triggered by the user.

您可以写从而改变价值和做你需要做其他...

You can either write a function which changes the value and does whatever else you need to do...

function changeTextarea(str) {
   document.getElementById('a').value = str;
   // Whatever else you need to do.
}

...或者轮询文本区域的价值...

...or poll the textarea's value...

(function() {
   var text = getText();   
   var getText = function() {
      return document.getElementById('a').value;
   }  

   setInterval(function() {
      var newtext = getText();
      if (text !== newText) {
          // The value has changed.
      }
      text = newText; 
   }, 100);
})();

...或者明确自己叫的onchange()事件。

document.getElementById('a').onchange();

(假设您将事件与的onchange 属性。)

变通办法不是非常伟大的解决方案。无论哪种方式,你需要做一些干预更新文本区域的 属性时触发额外的code

The workarounds are not terribly great solutions. Either way, you need to do some intervention to trigger extra code when updating a textarea's value property.

这篇关于如何检测从JavaScript文本框更改事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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