Dojo在以程序方式更改值时选择onChange事件触发 [英] Dojo Select onChange event firing when changing value programatically
问题描述
我有一个dojo(dijit)选择下拉菜单,调用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 programatically 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 programatically 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);
}
谢谢,
Justin
Thanks, Justin
推荐答案
我认为在这种情况下正确的修复将是 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);
}
}
});
请注意,我没有写这个函数,我很高兴地从源代码剽窃了最后一行,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屋!