我有一个文本区域字段,其值是以编程方式设置的。我想知道该值何时设置并在设置值后执行进一步的操作。我试过.on("change"), .bind("input propertychange")
但似乎没有任何作用。有什么建议吗?
<textarea id="testField"></textarea>
$("#testField").on("change keyup", function() {
// your code here
alert("Changed");
});
干杯。
最佳答案
似乎更改事件仅在用户与 UI 直接交互时才会触发。
重新定义触发特定 textarea 元素的更改事件的 value 属性对我有用:
// A specific instance of HTMLTextAreaElement
var txt = document.getElementById('txtDescripcion');
// define value property for your input instance
Object.defineProperty(txt, 'value', {
set: function(newValue) {
var valueProp = Object.getOwnPropertyDescriptor(HTMLTextAreaElement.prototype, 'value');
var oldValue = valueProp.get.call(txt);
valueProp.set.call(txt, newValue);
// Trigger the change event if value changed
if (oldValue != newValue) {
txt.dispatchEvent(new InputEvent('change'));
}
},
get: function() {
var valueProp = Object.getOwnPropertyDescriptor(HTMLTextAreaElement.prototype, 'value');
return valueProp.get.call(txt);
}
});
关于jquery - 以编程方式检测文本区域值更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36953235/