如何检测从JavaScript文本框更改事件 [英] How to detect textbox change event from 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屋!