我需要更改小部件dijit/form/TextBox
的属性value
,但我注意到每当我使用widget.set('value ','some value');
事件 change
在小部件上触发。
我需要以编程方式正确更改它,但不触发 change
事件。知道如何实现它吗?
https://jsfiddle.net/s5620bwd/6/
require(['dijit/form/TextBox', 'dojo/on'], function(TextBox, on) {
var textBox = new TextBox({
intermediateChanges: true
}, 'button');
on(textBox, 'change', function(value) {
this.set('value', value);
console.log(this.get('value')); // widget updated
});
// change value programmatically
textBox.set('value', 'value change programmatically');
});
最佳答案
TextBox 小部件有一个名为 textbox
的属性,它对应于小部件中包含的输入元素。如果您在元素上设置值,它将不会触发任何事件监听器。
require(['dijit/form/TextBox', 'dojo/on'], function(TextBox, on) {
var textBox = new TextBox({
intermediateChanges: true
}, 'button');
textBox.on('change', function(value) {
// this event handler will not fire
this.set('value', value);
console.log(this.get('value'));
});
textBox.textbox.value = 'my new value';
});
此外,要将事件处理程序附加到小部件,您应该使用小部件自己的 on
方法来附加处理程序。
关于javascript - 为 dijit/form/TextBox 正确设置值时如何避免触发事件 'change'?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35796983/