以编程方式更改值时,Dojo选择onChange事件触发 [英] Dojo Select onChange event firing when changing value programmatically
问题描述
我有一个dojo(dijit)select下拉列表,它调用js函数onChange。我期望它仅在用户更改下拉列表中的值时才调用onChange函数,但是,当我以编程方式从js代码更改下拉列表的值时,它甚至调用onChange函数。我如何才能仅在用户更改下拉值时才调用该函数?当我以编程方式更改值时,它不应调用该函数。
I have a dojo (dijit) select dropdown that calls a js function onChange. I was expecting this to only call the onChange function when the user changes the value in the dropdown, however, it even calls the onChange function when I programmatically change the value of the dropdown from js code. How do I get it to only call the function when the user changes the dropdown value? It shouldn't call the function when I programmatically change the value.
<select jsId="ddlBoundaryType" id="ddlBoundaryType" name="ddlBoundaryType"
dojoType="dijit.form.Select">
<option value="0">Circle</option>
<option value="1">Polygon</option>
</select>
dojo.addOnLoad(InitBoundaries);
function InitBoundaries() {
dojo.connect(dijit.byId("ddlBoundaryType"), 'onChange', Boundaries_ChangeBoundaryType);
}
推荐答案
我认为这种情况对您来说是 http://bugs.dojotoolkit.org/ticket/10594 ,因为它直接处理dijit.form.Select。当然,有几种方法可以解决此问题。
I think the proper fix in this case for you would be http://bugs.dojotoolkit.org/ticket/10594, since it deals directly with dijit.form.Select. Of course, there are a few ways to fix this.
- 升级dojo:)。
- 继承dijit.form.Select并修补 _updateSelection函数。
- 扩展dijit.form.Select并将其直接修补在那里。
我会放弃第一个。第二种方法和第三种方法相似,因此我将使用第三种方法发布一个简单的解决方法,
I will forgo the first. The the second and the third method are similar, so I will just post a simple fix using the third way,
dijit.form.Select.extend({
_updateSelection: function() {
this.value = this._getValueFromOpts();
var val = this.value;
if(!dojo.isArray(val)){
val = [val];
}
if(val && val[0]){
dojo.forEach(this._getChildren(), function(child){
var isSelected = dojo.some(val, function(v){
return child.option && (v === child.option.value);
});
dojo.toggleClass(child.domNode, this.baseClass + "SelectedOption", isSelected);
dijit.setWaiState(child.domNode, "selected", isSelected);
}, this);
}
}
});
请注意,我没有编写此函数,但我很高兴在源代码中将其the窃为最后一行,则this._handleOnChange(this.value)已删除。
Note that I did not write this function, I happily plagiarized it from the source code with the last line, this._handleOnChange(this.value) removed.
myWidget.attr('value', newValue, false) // should now work without firing onChange.
这篇关于以编程方式更改值时,Dojo选择onChange事件触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!